jQuery如何处理注释元素? [英] How does jQuery treat comment elements?

查看:470
本文介绍了jQuery如何处理注释元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直认为jQuery只对DOM 元素进行操作,即 nodeType == 1 的节点。

I always thought that jQuery operates only on DOM elements, that is those nodes that have nodeType == 1.

但是,我很震惊,在创建HTML $(< p>< / p>< !- comment - >) / code>导致:

However I'm shocked that while creating HTML $("<p> </p><!-- comment -->") results in:

[p,Comment {data =comment,length = 21,nodeName =#comment ,更多...}] (Firebug格式化)

[p, Comment { data=" comment ", length=21, nodeName="#comment", more...}] (Firebug formatting)

我接受了AJAX的一些HTML,某个函数只适用于元素: defaultView.getComputedStyle(elem,null)

I accepted some HTML by AJAX and a DOM Comment was created this way and passed somewhere to a function that is applicable only to elements: defaultView.getComputedStyle( elem, null )

推荐答案


我一直认为jQuery只能操作DOM元素

I always thought that jQuery operates only on DOM elements

其选择器仅选择 DOM元素。在你的情况下,你从你提供的HTML字符串创建节点。所以jQuery解析字符串并返回你要求的节点。

Its selectors only select DOM elements. In your case, you're creating nodes from the HTML string you've provided. So jQuery parses the string and gives you back the nodes you're asking for.

要清理它,请执行 .filter() code>。

To clean it, do a .filter().

var els = $("<p> </p><!-- comment -->").filter(function() { 
                                                  return this.nodeType === 1; 
                                               });

这篇关于jQuery如何处理注释元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆