7--DOM operation in Jquery (find node)

Find element nodes (labels) : use the selector in Jquery to complete.
Find the attribute node : After finding the element node, you can use the attr() method of the Jquery object to obtain each attribute of the element node.
Example 1:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>查找属性节点</title>
    <!--导入Jquery框架-->
    <script src="jquery-1.11.3.js"></script>
</head>
<body>
    <p title="选择你新欢的水果">你新欢的水果是?</p>
    <ul>
        <li title="苹果">苹果</li>
        <li title="橘子">橘子</li>
        <li title="香蕉">香蕉</li>
        <li title="菠萝">菠萝</li>
    </ul>
    <script>
        $(function(){
    
    
            /*获取p元素*/
            var $_p=$("p");
            /*获取p标签的title属性的值*/
            var $_title=$_p.attr("title");
            alert($_title);
        });

        $(function(){
    
    
            /*获取第二个li元素*/
            var $_lis=$("li:eq(1)");
            var $_jztitle=$_lis.attr("title");
            alert($_jztitle);
        });
    </script>
</body>
</html>

Find a text node : use the text() method
Example 2:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>查找文本节点</title>
    <!--导入Jquery框架-->
    <script src="jquery-1.11.3.js"></script>
</head>
<body>
    <p title="选择你新欢的水果">你新欢的水果是?</p>
    <ul>
        <li title="苹果">苹果</li>
        <li title="橘子">橘子</li>
        <li title="香蕉">香蕉</li>
        <li title="菠萝">菠萝</li>
    </ul>
    <script>
        $(function(){
    
    
            /*获取p元素*/
            var $_p= $("p");
            /*获取p元素中的文本*/
            var p_text=$_p.text();
            alert(p_text);
        });
    </script>
</body>
</html>

Please leave your thumbs upInsert picture description here

Guess you like

Origin blog.csdn.net/qwy715229258163/article/details/113865982