jquery 获取包含文本和注释节点在内的所有子节点,使用 contents() 方法,children() 方法不返回文本节点
测试代码
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="js/jquery-3.2.1.min.js"></script>
</head>
<body>
<p id="app">
<span>如此好花如此月,</span>
莫将花月作寻常
</p>
<button id="getAll">获取全部contents方法</button>
<br><br>
<button id="get">获取children方法</button>
<script>
$("#getAll").click(function() {
console.log($("p").contents())
})
$("#get").click(function() {
console.log($("p").children())
})
</script>
</body>
</html>
运行效果
至此完