首页 > 如何利用nodeType信息编写函数来获取<body>区域的全部HTML注释

如何利用nodeType信息编写函数来获取<body>区域的全部HTML注释

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>To-Do List</title>


</head>

<body>


<h1>Things To Do</h1>

<!--h1标题信息-->


<ol id="toDoList">
    <li>Mow the lawn</li> <!--第一个li信息--> 
    <li>Clean the windows</li><!--第二个li信息-->
    <li>Anwer your email</li><!--第三个li信息-->
  </ol>




<p id="toDoNotes">Make sure all these are completed by 8pm so you can watch the game on TV!</p>


</body>
</html>

以下是一个例子:

var $comments=$('#toDoList').contents().filter(function(){
  return this.nodeType === 8;
});
// comments 也就是你需要的内容
console.log($comments);

这是一个 DEMO


BTW: 帮修改了问题内容,请希望得到解答的题主之后在提问后,哪怕把自己的问题瞟一眼呢。另外,如果提问有关Web前端的问题,可以会使用以下三个工具,这会让别人更容易理解你的问题,并有一个最接近问题的调试环境。

  1. http://jsfiddle.net
  2. http://jsbin.com
  3. http://runjs.cn
【热门文章】
【热门文章】