简体   繁体   English

如何检查jQuery中是否存在具有id的元素?

[英]How to check if an element with id exists or not in jQuery?

I'm generating a div dynamically and I've to check whether a dynamically generated div exists or not ? 我正在动态生成div,我要检查是否存在动态生成的div? How can I do that? 我怎样才能做到这一点?

Currently I'm using the following which does not detects the div generated dynamically. 目前我正在使用以下内容,它不会检测动态生成的div。 It only detects if there is already an element with the id contained in the HTML template. 它仅检测HTML模板中是否已存在包含id的元素。

$(function() {
    var $mydiv = $("#liveGraph_id");
    if ($mydiv.length){
        alert("HHH");
    }
});

How can I detect the dynamically generated div? 如何检测动态生成的div?

If mutation observes aren't an option due to their browser compatibility , you'll have to involve the code that's actually inserting the <div> into the document . 如果由于浏览器兼容性而导致变异观察不是一种选择,那么您将不得不涉及实际将<div>插入document

One options is to use a custom event as a pub/sub . 一种选择是将自定义事件用作pub / sub

$(document).on('document_change', function () {
    if (document.getElementById('liveGraph_id')) {
        // do what you need here
    }
});
// without a snippet to go on, assuming `.load()` for an example
$('#container').load('/path/to/content', function () {
    $(this).trigger('document_change');
});

If it is added dinamically, you have to test again. 如果它是dinamically添加,你必须再次测试。 Let's say, a click event 比方说,点击事件

$("#element").click(function()
{
    if($("#liveGraph_id").length)
        alert("HHH");
});

How you inserting your dynamic generated div? 如何插入动态生成的div?

It works if you do it in following way: 如果您按以下方式执行此操作:

var div = document.createElement('div');
div.id = 'liveGraph_id';
div.innerHTML = "i'm dynamic";
document.getElementsByTagName('body')[0].appendChild(div);
if ($(div).length > 0) {
    alert('exists'); //will give alert
}
if ($('#liveGraph_id').length > 0) {
    alert('exists'); //will give alert
}
if ($('#liveGraph_id_extra').length > 0) {
    alert('exists'); //wont give alert because it doesn't exist.
}

jsfiddle . jsfiddle

Just for interest, you can also use a live collection for this (they are provided as part of the DOM). 只是为了感兴趣,您还可以使用实时集合(它们作为DOM的一部分提供)。 You can setup a collection of all divs in the page (this can be done in the head even before the body is loaded): 您可以在页面中设置所有div的集合(这可以在加载正文之前在头部完成):

var allDivs = document.getElementsByTagName('div');

Any div with an id is available as a named property of the collection, so you can do: 任何具有id的div都可以作为集合的命名属性,因此您可以:

if (allDivs.someId) {
  // div with someId exists
}

If the ID isn't a valid identifier, or it's held in a variable, use square bracket notation. 如果ID不是有效标识符,或者它保存在变量中,请使用方括号表示法。 Some play code: 一些播放代码:

<button onclick="
  alert(!!allDivs.newDiv);
">Check for div</button>
<button onclick="
  var div = document.createElement('div');
  div.id = 'newDiv';
  document.body.appendChild(div);
">Add div</button>

Click the Check for div button and you'll get false . 单击Check for div按钮,您将得到false Add the div by clicking the Add div button and check again—you'll get true . 通过单击添加div按钮添加div并再次检查 - 您将获得true

is very simple as that 很简单

   if(document.getElementById("idname")){
//div exists 

}

or 要么

    if(!document.getElementById("idname")){
//  don't exists
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM