有什么方法可以找到 documentFragment 中的元素吗?
var oFra = document.createDocumentFragment();
// oFra.[add elements];
document.createElement("div").id="myId";
oFra.getElementById("myId"); //not in FF
在将片段附加到文档之前如何获取“myId”?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
所有这些答案都相当古老,从那时起
querySelectorAll
和querySelector
还没有广泛使用。应该注意的是,这两个接受 CSS 选择器作为参数的函数确实可以在现代浏览器中的 DocumentFragment 上工作,并且应该是处理问题中情况的首选方法。对于不支持querySelectorAll
或querySelector
的旧版浏览器,某些答案中提出的替代解决方案将是一个好方法。这是一个示例用法:
一个好的实现应该首先使用对象检测来查看浏览器是否支持此功能。例如:
All of these answers are rather old, from back when
querySelectorAll
andquerySelector
were not widely available. It should be noted that these two functions which accept CSS selectors as parameters do work onDocumentFragment
s in modern browsers, and should be the preferred way of dealing with the situation in the question. The alternate solutions presented in some of the answers would be a good approach for legacy browsers which did not supportquerySelectorAll
orquerySelector
.Here is an example usage:
A good implementation should first use object detection to see if the browser supports this. For instance:
不。
DocumentFragment
API 至少可以说是最小的:它没有定义属性或方法,这意味着它只支持Node
API。由于getElementById
等方法是在Document
API,它们不能与DocumentFragment
一起使用。No. The
DocumentFragment
API is minimal to say the least: it defines no properties or methods, meaning that it only supports the properties and methods defined in theNode
API. As methods such asgetElementById
are defined in theDocument
API, they cannot be used with aDocumentFragment
.NickFitz 是对的,在标准或浏览器中,
DocumentFragment
没有您期望从Document
或Element
获得的 API(这是一个遗憾; 能够设置片段的innerHTML
会非常方便,即使是框架也无法帮助您,因为它们往往要求节点位于文档中,或者以其他方式使用上下文节点上的方法。您可能必须自己编写,例如:
在进行过程中跟踪引用几乎肯定比依赖像上面这样的幼稚、性能不佳的函数要好。 。
NickFitz is right,
DocumentFragment
doesn't have the API you expect fromDocument
orElement
, in the standard or in browsers (which is a shame; it would be really handy to be able to set a fragment'sinnerHTML
.Even frameworks don't help you here, as they tend to require Nodes be in the document, or otherwise use methods on the context node that don't exist on fragments. You'd probably have to write your own, eg.:
It would almost certainly be better to keep track of references as you go along than to rely on a naïve, poorly-performing function like the above.
怎么样:
除非您已将创建的
div
添加到文档片段中,否则我不确定为什么getElementById
会找到它?--edit
如果您愿意推出自己的 getElementById 函数,那么您应该能够获得所需的引用,因为此代码有效:
What about:
Unless you've added the the created
div
to your document fragment I'm not sure whygetElementById
would find it?--edit
If you're willing to roll your own getElementById function then you ought to be able to get the reference you're after, because this code works:
使用 jQuery:
jsFiddle: http://jsfiddle.net/CCkFs/
您需要创建 div 的开销不过,jQuery。有点老套,但它有效。
Using jQuery:
jsFiddle: http://jsfiddle.net/CCkFs/
You have the overhead of creating the div with jQuery, though. A little hacky, but it works.
到目前为止,找出可以使用
DocumentFragment
做什么和不能做什么的最好方法是检查它的原型:我明白
...这告诉我我可以做这样的事情:
PS这不起作用:
...你会被告知“该对象没有实现 DocumentFragment 接口”
The best way by far to find out what you can and can't do with a
DocumentFragment
is to examine its prototype:I get
... which tells me I can do things like:
PS this won't work:
... you'll be told that "the object doesn't implement the DocumentFragment interface"
下面列出的外部源显示了以下代码片段:
其中显示了设置对象 ID 参数的不同方式。
Javascript 套件 - 文档对象方法
An external source, listed below, showed the following code snippet:
Which displays a different way of setting the object's ID parameter.
Javascript Kit - Document Object Methods
我的 DOM 在元素标签下有一个 #document-fragment 。
这就是我正在使用的(使用 jQuery),我还有一个用例,我在字符串中包含 HTML DOM -
My DOM has a #document-fragment under the element tag.
This is what I am using (using jQuery) , Also I have a use case where I have the HTML DOM in a string -