Home  >  Article  >  Web Front-end  >  How to use jQuery to modify HTML element content

How to use jQuery to modify HTML element content

PHPz
PHPzOriginal
2023-04-17 10:30:101294browse

JQuery is a popular JavaScript library designed to easily find elements in HTML documents and then operate on them. In this article, we will discuss how to use jQuery to modify the content of an HTML element (i.e., element), and this modification is achieved by a user click (i.e., click).

Step One: Preparation

Before we start writing jQuery code, we need to prepare our HTML page. The following is a simple HTML page example, which contains a < span > element and a button:

<!DOCTYPE html>
<html>
  <head>
    <title>jquery demo</title>
    <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
    <script>
      $(document).ready(function(){
        $("#clickButton").click(function(){
          $("span").text("你点击了按钮!");
        });
      });
    </script>
  </head>
  <body>

    <h1>JQuery事件点击修改span内容</h1>
    <p>点击下面的按钮!</p>

    <button id="clickButton">点击这里</button>

    <p>点这里看看下面<span>会变化</span>!</p>

  </body>
</html>

In this example, we introduced the jQuery library and defined an element that is already the same as < span > ;The click event listener associated with the element. This event listener will be triggered when the user clicks the button, and changes the text content of the < span > element to "You clicked the button!"

Now, let's explain the meaning of this code one by one .

Step 2: jQuery event binding

$(document).ready() function is one of the special functions of jQuery, which is used to execute our code when the page is ready. This means that the code inside the $(document).ready() function will be executed as soon as the DOM tree is loaded. Therefore, we need to bind a function for our click event in this block.

We use $("#clickButton").click() to bind a click event, which will be triggered when the user clicks a button with the ID "clickButton". Note that we can also use class selectors or tag selectors to match HTML elements, but in this case, we are using the ID selector.

Inside the click event handler, we use the $ ("span").text() method to change the text content of the < span > element to "You clicked the button!". We passed a string parameter in the method, "You clicked the button!", so that we can easily change the text content of the < span > element.

Step Three: Test Results

Now, we open the page containing the above HTML code and click the button, you can observe that the text content in the < span > element has changed , as shown below:

JQuery事件点击修改span内容

点击下面的按钮!

(点击按钮后)

点这里看看下面你点击了按钮!

In this example, we are using very basic jQuery code, but this gives us a good starting point for understanding how to use jQuery to modify elements on an HTML page. If necessary, you can extend this code example to suit your needs.

The above is the detailed content of How to use jQuery to modify HTML element content. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn