jquery 遍历 contents()

contents()

找出匹配元素集合中所有的子节点(包括文本节点),如果集合中元素为‘iframe’则返回该元素的内容

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  
  <script>
  $(document).ready(function(){
    $("p").contents().not("[nodeType=1]").wrap("<b/>");
  });
  </script>
  
</head>
<body>
  <p>Hello <a href="http://ejohn.org/">John</a>, how are you doing?</p>
</body>
</html>

 

$("p").contents().not("[nodeType=1]").wrap("<b/>");
得到匹配p元素集合中所有的子节点,然后排除p元素nodetype属性值为1的匹配p元素,给最终得到的所有子节点加上‘<b/>’,以下是匹配p元素节点

Hello  ,how are you doing?

你可能感兴趣的:(html,jquery)