【发布时间】:2011-08-28 12:20:55
【问题描述】:
比如说你有 div:
<div id="hello"></div>
你正在动态创建一个 div:
<div id="hello"></div>
您是否可以在 Jquery 中使用一个函数来检查页面上是否已经存在具有您尝试创建的 ID 的对象?
【问题讨论】:
标签: jquery jquery-selectors javascript
比如说你有 div:
<div id="hello"></div>
你正在动态创建一个 div:
<div id="hello"></div>
您是否可以在 Jquery 中使用一个函数来检查页面上是否已经存在具有您尝试创建的 ID 的对象?
【问题讨论】:
标签: jquery jquery-selectors javascript
对于 jQuery 方法,您可以使用
if($("#selector").length) {
//object already exists
}
【讨论】:
if (document.getElementById('hello')) {
// yup, already there
}
或者,jQuery方式:
if ($('#hello').length) {
// yup, already there
}
【讨论】:
($('#hello')[0])