我正在使用 jQuery 动态生成一堆像这样的 div
:
<div class="mydivclass" id="myid1">Some Text1</div>
<div class="mydivclass" id="myid2">Some Text2</div>
我的代码中的其他地方需要检测这些DIV是否存在。这些div的类名相同,但每个div的ID都会更改。有什么想法可以使用jQuery进行检测吗?
我正在使用 jQuery 动态生成一堆像这样的 div
:
<div class="mydivclass" id="myid1">Some Text1</div>
<div class="mydivclass" id="myid2">Some Text2</div>
我的代码中的其他地方需要检测这些DIV是否存在。这些div的类名相同,但每个div的ID都会更改。有什么想法可以使用jQuery进行检测吗?
if ($(".mydivclass")[0]){
// Do something if class exists
} else {
// Do something if class does not exist
}
如果在第一个索引位置([0]
)存在一个真值,那么就假定该类存在。
Edit 04/10/2013: 我已经创建了一个jsperf测试案例,在这里。
你可以使用size()
,但是jQuery推荐使用length以避免另一个函数调用的开销:
$('div.mydivclass').length
所以:// since length is zero, it evaluates to false
if ($('div.mydivclass').length) {
更新
所选答案使用了性能测试,但它略有缺陷,因为它还将元素选择作为性能的一部分,而这并不是在这里被测试的内容。这里是一个更新后的性能测试:
http://jsperf.com/check-if-div-exists/3
我的第一次运行测试显示属性检索比索引检索更快,尽管我认为差别微乎其微。我仍然更喜欢使用length,因为对我来说它更符合代码意图,而不是更简洁的条件。
.length
目前提供了最佳的平均性能。 - user110857使用本地 JavaScript 总是更快。在这个例子中:(示例)
if (document.querySelector('.mydivclass') !== null) {
// .. it exists
}
如果你想检查父元素是否包含另一个特定类的元素,你可以使用以下任一方法。 (示例)
var parent = document.querySelector('.parent');
if (parent.querySelector('.child') !== null) {
// .. it exists as a child
}
或者,您可以在父元素上使用 .contains()
方法。(示例)
var parent = document.querySelector('.parent'),
child = document.querySelector('.child');
if (parent.contains(child)) {
// .. it exists as a child
}
最后,如果您想检查一个给定的元素是否仅包含特定的类,请使用:
if (el.classList.contains(className)) {
// .. el contains the class
}
$('div').hasClass('mydivclass')// Returns true if the class exist.
div
元素的数组,然后返回循环遍历它们以查看它们是否有该类的结果,最后将该数组丢弃)。 - T.J. CrowderhasClass
比这里的其他选择器快33%。 查看http://jsperf.com/hasclass00 - Husseindiv
元素)中,才能回避我强调的问题(构建数组)。试着用一堆divs:http://jsperf.com/hasclass00/2 在我的Chrome副本上慢63%,在我的Firefox副本上慢43%,在Opera上慢98%(!)。但更重要的是,构建一个div列表然后搜索它比提供选择器引擎所需的所有信息要慢,这是有道理的。 - T.J. Crowder以下是不使用Jquery的解决方案
var hasClass = element.classList.contains('class name to search');
// hasClass is boolean
if(hasClass === true)
{
// Class exists
}
参考 链接
这很简单...
if ($('.mydivclass').length > 0) {
//do something
}
为了显式地测试 div
元素:
如果( $('div.mydivclass').length ){...}
.mydivclass
稍微慢一些。 - Stefan Kendalldiv
部分的人。 - T.J. Crowder以下是一些方法:
1. if($("div").hasClass("mydivclass")){
//Your code
//It returns true if any div has 'mydivclass' name. It is a based on the class name
}
2. if($("#myid1").hasClass("mydivclass")){
//Your code
// It returns true if specific div(myid1) has this class "mydivclass" name.
// It is a based on the specific div id's.
}
3. if($("div[class='mydivclass']").length > 0){
//Your code
// It returns all the divs whose class name is "mydivclass"
// and it's length would be greater than one.
}
代码如下:
if ($('.mydivclass').length > 0) {
//Things to do if class exist
}
To hide the div with particuler id :
if ($('#'+given_id+'.mydivclass').length > 0) {
//Things to do if class exist
}
最好的方法是如下所示检查类的长度:
if ($('.myDivClass').length) {
:first
会提高性能(不知道 @itgorilla 是否重视性能),但它是否提高性能取决于浏览器,这可能是因为它改变了 jQuery 可以用来进行选择的本地特性。这里是一个存在 div 的测试案例,这里是一个不存在 div 的测试案例。 - T.J. Crowderif (!$(".mydivclass")[0]){ /* do stuff */ }
。 - Shaz