How to check if an object with an ID already exists on the page? [duplicate]

喜你入骨 提交于 2019-12-08 14:35:39

问题


Possible Duplicate:
Is there an “exists” function for jQuery

Say for instance you have the div:

<div id="hello"></div>

And you are dynamically creating a div:

<div id="hello"></div>

Is there a function you can use in Jquery which will check to see if the object with the ID you are trying to create already exists on the page?


回答1:


For jQuery method you could go with

if($("#selector").length) {
    //object already exists
}



回答2:


if (document.getElementById('hello')) {
    // yup, already there
}

Or, the jQuery way:

if ($('#hello').length) {
    // yup, already there
}


来源:https://stackoverflow.com/questions/6052872/how-to-check-if-an-object-with-an-id-already-exists-on-the-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!