javascript 如何使用jQuery检查动态创建的元素是否已经存在?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8777612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if a dynamically created element already exists using jQuery?
提问by Omid
I create an element dynamically:
我动态创建一个元素:
var $el = $('<div class="someclass"></div>');
I want to append the element ($el
) somewhere, but it shouldn't have an id
.
我想在$el
某处附加元素 ( ),但它不应该有id
.
How do I know if it has been appended before or not?
我怎么知道它之前是否被附加过?
edit
I thought this should work
编辑
我认为这应该有效
if($($el, "#target").length == 0)
$("#target").append($el);
but that was wrong
但那是错误的
回答by Dennis
You can simply check to see if it has a parent:
您可以简单地检查它是否有父项:
var $el = $('<div class="someclass"></div>');
//Code you wrote that may or may not attach it to the DOM
if($el.parent().length === 0) {
//The element has not been added to the DOM because it doesn't not have a parentNode
}
However, if you have no idea what is actually being done with the element, you may have other problems with your code.
但是,如果您不知道该元素实际执行了什么操作,则您的代码可能存在其他问题。
回答by Michael Berkowski
If I understand, and you need to check if another <div class='someclass'>
already exists before appending $el
, you can do:
如果我理解,并且您需要<div class='someclass'>
在 append 之前检查是否已经存在另一个$el
,您可以执行以下操作:
if ($("div.someclass").length === 0) {
// it has not been created yet, create a new one
var $el = $('<div class="someclass"></div>');
}
To check if it is already a child node of some other element, use .find()
要检查它是否已经是某个其他元素的子节点,请使用 .find()
if ($("#parentNode").find("div.someclass").length === 0 {
// it has not been created yet, create a new one
var $el = $('<div class="someclass"></div>');
}
回答by Alnitak
If I understand you correctly, you wish to find whether this exactelement is already in the DOM, rather than some other arbitrary element that happens to have the same class.
如果我理解正确的话,您希望找到这个确切的元素是否已经在 DOM 中,而不是其他一些碰巧具有相同类的任意元素。
If so, I believe this will work.
如果是这样,我相信这会奏效。
First, get the originally created element as an HTMLElement
object rather than a jQuery object:
首先,将最初创建的元素作为HTMLElement
对象而不是 jQuery 对象获取:
var el = $el.get(0);
then try to .find()
it in the doc:
然后.find()
在文档中尝试:
var $match = $(document).find(el);
var found = ($match.length > 0);
this won't be efficient, though - restrict the selector document
to a narrower part of your DOM if you can!
但是,这不会很有效 -document
如果可以的话,将选择器限制在DOM 的较窄部分!
Your edit suggests you could use #target
as that selector.
您的编辑建议您可以#target
用作该选择器。
回答by Jay Gilford
If you only want one element, you should really be using an ID with the element, and then checking for it in a similar way to how Michael has above. using
如果你只想要一个元素,你真的应该在元素中使用一个 ID,然后以与上面 Michael 类似的方式检查它。使用
var el = $('<div id="someID" class="someClass" />');
I'm also not sure why you are using $el
instead of just el
我也不确定你为什么要使用$el
而不仅仅是el