开发者

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

开发者 https://www.devze.com 2023-03-06 18:09 出处:网络
This question already has answers here: Closed 11 years ago. Possibl开发者_Python百科e Duplicate: Is there an “exists” function for jQuery
This question already has answers here: Closed 11 years ago.

Possibl开发者_Python百科e 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?


For jQuery method you could go with

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


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

Or, the jQuery way:

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

精彩评论

暂无评论...
验证码 换一张
取 消