这是html代码,
<li class="comment-content">
Target String!!!
<span class="comment-time">2011-07-13 17:08:39</span>
<a title="comment" class="bottoml cof-comment" href="#">comment</a>
<a title="modify" class="bottoml cof-comment" href="#">modify</a>
<a title="delete" class="bottoml cof-comment" href="#">delete</a></li>
</li>
Run Code Online (Sandbox Code Playgroud)
我可以使用像这样的jquery来获取comment-content的文本,
$(".comment-content").text();
Run Code Online (Sandbox Code Playgroud)
结果是:
Target String!!! 2011-07-13 17:08:39 comment modify delete
Run Code Online (Sandbox Code Playgroud)
我的问题,我可以得到字符串"Target String !!!" 只使用jquery?没有子标签的文本,我只是指直接子文本.
像这样:
$('.comment-content').contents().filter(function(){ return this.nodeType == 3; });
Run Code Online (Sandbox Code Playgroud)
示例:http://jsfiddle.net/AlienWebguy/FGAXE/
正如您在MDN Node.nodeType解释中看到的那样,常量TEXT_NODE = 3,这就是我们需要查找的内容.
| 归档时间: |
|
| 查看次数: |
247 次 |
| 最近记录: |