How to use jquery to get text or objects in web pages

In the web page, we sometimes need to change a specific text or value in the front end. We can use the selector in jquery to achieve the desired operation. code show as below:

//html code body part
<p title="Furniture">Furniture</p>
<ul>
    <li title="Chair">Chair (0)</li>
    <li title="床">床(<span>0</span>)</li>
</ul>

//To achieve this function, the DOM operation of jquery is used
var $li=$("ul li:eq(0)");//This sentence is to get the first <li> node in <ul>
var li_txe=$li.text();//This sentence is to get the text content in the node
alert(li_txt);//This sentence is to print the text content
//If you want to directly change the text information you get,
$li.text(fill in the changed content)//This will change the part you selected to fill in the changed content
//If you want to direct the numeric part of the text you can add a tag to the part you need to rewrite like the span tag I explained in the above example
$(span).text(5)//;//Change the number in the span tag to 5


jQuery's selector is very powerful. When looking for elements, it can not only find element nodes, but also attribute nodes.

var $para=$("p");//Get the p node
var p_txt=$para.attr("title");//Get the attribute title of the <p> element node
alert(p_txt)//Print title attribute value

The above are the two simplest dom operations.
If you want to add some content to the dom node of the web page, you need to create a node.
var $li_1 =$("<li>table</li>");//Create a li element, including element nodes and text nodes, where the table is the text node
var $li_2=$("<li>sofa</li>");///Create a li element, including element nodes and text nodes, where the table is the text node
$("ul").append($li_1);//The selector selects the ul node, and adds the first one created to the web page to display it
$("ul").append($li_2);//The selector selects the ul node, and adds the second one created to the web page to display it
//The above method is to create a text node. In fact, when creating an element node, the text content is directly written out, and the append() method is to add.


Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326668553&siteId=291194637