Getting Started with jQuery: Simplify Web Development with the JavaScript Library

jQuery has become one of the most popular JavaScript libraries in web development today. It helps simplify tasks such as HTML document traversal, event handling, animation, and AJAX operations, and is widely used in various websites and web applications.

jQuery is open source and can be used by introducing related library files on web pages. Here's a simple example that demonstrates how to use jQuery to create a "Hello World!" alert box that pops up when a button is clicked:

<!DOCTYPE html>
<html>
<head>
    <title>jQuery Example</title>
    <!-- 引入 jQuery 的库文件 -->
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
    <button id="myButton">Click me!</button>

    <script>
        $(document).ready(function() {
            // 在按钮上添加点击事件处理程序
            $('#myButton').click(function() {
                // 显示警告框
                alert("Hello World!");
            });
        });
    </script>
</body>
</html>

Let's explain the code in this example line by line:

  • $(document).ready() A function is a function that is executed after the page has loaded. This is to ensure that all DOM elements have been loaded and prevent errors when the code tries to manipulate elements that have not been fully loaded.
  • Selectors  $() are a major feature of jQuery. In this example, we use $('#myButton')to select the button element with id "myButton".
  • .click() A function is an event handler that performs some action when the button is clicked.
  • alert() The function displays a message in an alert box.

In addition to simple interaction, jQuery also provides many useful functions. For example, in the following example we will use jQuery to send an AJAX request to the server and update the page content based on the response:

<!DOCTYPE html>
<html>
<head>
    <title>jQuery AJAX Example</title>
    <!-- 引入 jQuery 的库文件 -->
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
    <div id="myDiv">Loading...</div>

    <script>
        $(document).ready(function() {
            // 发送 AJAX 请求
            $.ajax({
                url: "https://jsonplaceholder.typicode.com/todos/1",
                success: function(data) {
                    // 更新页面内容
                    $('#myDiv').html(JSON.stringify(data));
                }
            });
        });
    </script>
</body>
</html>

In this example, we use $.ajax()functions to make AJAX requests to a remote server. This function allows us to specify the requested URL, HTTP method, data, etc. When the server responds successfully, successthe callback function will be executed. In this function, we use the jQuery selector $('#myDiv')to select the element with id "myDiv" and update its content to display the JSON data fetched from the server.

Overall, jQuery is a powerful, easy-to-use, and widely accepted JavaScript library. Using jQuery, web developers can more easily manage HTML documents, perform animations, handle events and send AJAX requests and other tasks.

Guess you like

Origin blog.csdn.net/weixin_69420643/article/details/130824330