比如说你有div:
<div id="hello"></div>
你正在动态创建一个div:
<div id="hello"></div>
是否有一个可以在Jquery中使用的函数,它将检查页面上是否存在具有您要创建的ID的对象?
答案 0 :(得分:14)
if (document.getElementById('hello')) {
// yup, already there
}
或者,jQuery方式:
if ($('#hello').length) {
// yup, already there
}
答案 1 :(得分:12)
对于jQuery方法,您可以使用
if($("#selector").length) {
//object already exists
}