Home > Web Front-end > JS Tutorial > body text

Detailed explanation of jQuery's ability to dynamically add nodes and traverse nodes

小云云
Release: 2017-12-23 10:04:39
Original
2496 people have browsed it

This article mainly introduces jQuery's functions of dynamically adding nodes and traversing nodes. It analyzes jQuery's dynamic addition and traversal related operation techniques for page element node elements in the form of examples. Friends who need it can refer to it. I hope it can help everyone.

Dynamicly add nodes:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title></title>
  <script src="jquery-1.10.2.js" type="text/javascript"></script>
  <script type="text/javascript">
    $(function () {
      var link = $("<a href=&#39;http://www.baidu.com&#39;>百度</a>"); //创建一个节点
      $("p:first").append(link); //将link节点添加到第一个p中
      var link = $("<a href=&#39;http://www.google.com.hk&#39;>谷歌</a>"); //重新创建一个节点
      $("p:last").append(link); //将link节点添加到最后一个p中
    })
    //======================================================================================
    //定义一个字典风格的数组
    var data = { "百度": "http://www.baidu.com", "新浪": "http://www.sina.com.cn", "搜狐": "http://www.sohu.com" };
    $(function () {
      $.each(data, function (key, value) { //遍历这个数组,调用匿名函数进行处理
        var link = $("<tr><td><a href=" + value + " rel="external nofollow" >" + key + "</a></td></tr>"); //创建一个节点
        $("#table1").append(link); //将这个节点加入到table中
      })
    })
  </script>
</head>
<body>
<p></p>
<p></p>
<table id="table1"></table>
</body>
</html>
Copy after login

Traverse nodes:

HTML part:


<html>
<head>
  <title></title>
  <script src="jquery-1.10.2.min.js" type="text/javascript"></script>
</head>
<body>
  <p>AA</p>
  <p>BB</p>
  <p>CC</p>
  <p>DD</p>
  <p>EE</p>
  <p>FF</p>
  <p>KK</p>
</body>
</html>
Copy after login

js part:

 alert($(this).next("p").text()) //效果:当单击AA的时候会弹出BB,当点击BB的时候会弹出CC,当点击CC的时候会弹出空的警告框(因为CC这个p节点后挨着它的是p元素,所以就弹出一个空的警告框),当点击FF的时候会弹出KK,当点击KK的时候会弹出空的警告框,(因为KK这个p节点后没有同辈的p元素挨着它了,所以就弹出一个空的警告框)
  /*--nextAll()方法用于获取“节点之后”所有的同辈元素--*/
  $("p,p").click(function () {
    alert($(this).nextAll().text()); //当单击p标签或者p标签的时候弹出点击的当前标签后的所有标签的text();
  })
  $("p,p").click(function () {
    alert($(this).nextAll("p").text()); //当单击p标签或者p标签的时候弹出点击的当前标签后的所有p标签的text();
  })
  $("p").click(function () {
    $(this).nextAll("p").css("background", "red"); //当点击p标签的时候将它后面的所有p标签的背景都设为红色
  })
  $("p").click(function () {
    $.each($(this).nextAll("p"), function () { $(this).css("background-color", "red") })
  //当点击p标签的时候将它后面的所有p标签的背景都设为红色,与上面的那一条效果是一样的(解释:先取得当前点击的p标签后面的所有p标签,然后对它进行遍历,然后通过后面的匿名函数将取得的所有p标签的背景设为红色)注意这前后两个this意思是不一样的:前面的this指的是当前点击的p标签,后面的thi是:在获取到当前点击的p标签的“后面的p标签”后,遍历他们的每一个p,后面的thi是:在后面的匿名函数正在处理的“当前遍历到的p标签” 【前面的是当前点击的p,后面的匿名函数的真正处理的当前p】
  })
  $("p,p").click(function () {
    //$(this).css("background", "red"); $(this).siblings().css("background", "yellow"); //将当前点击的p或者P标签背景设为红色,其他的兄弟标签背景设为黄色
    $(this).css("background", "red").siblings().css("background", "yellow");//与上面一句等同
  })
})
</script>
Copy after login

Related recommendations:

Js method of dynamically adding node content to the form_javascript skills

jquery Introduction to several methods of adding nodes_jquery

PHP How to add nodes to XML_php tips

The above is the detailed content of Detailed explanation of jQuery's ability to dynamically add nodes and traverse nodes. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template