我正在寻找使用jQuery遍历的特定div,如果该div具有特定的类,则显示警告。http://jsfiddle.net/gfA4F/1/
下面是HTML(外部div用于模板):
<div class="thisness">
<div class="container">
<div class="btn-group dropdown options-btns mark-read">
<button type="button" class="btn btn-sm options-btn dropdown-toggle" data-toggle="dropdown">
<span class="glyphicon glyphicon-unchecked"></span>
</button>
<ul class="dropdown-menu" role="menu">
<li>>Item1</li>
<li>>Item2</li>
<li>>Item3</li>
</ul>
</div>
</div>
<div class="container">
<div class="btn-group dropdown options-btns mark-read">
<button type="button" class="btn btn-sm options-btn dropdown-toggle" data-toggle="dropdown">
<span class="glyphicon glyphicon-check"></span>
</button>
<ul class="dropdown-menu" role="menu">
<li>>Item1</li>
<li>>Item2</li>
<li>>Item3</li>
</ul>
</div>
</div>
...
</div>
这里是需要纠正的遍历尝试:
$(document).ready(function(){
$(".thisness").on("show.bs.dropdown", ".options-btns", function () {
var $this = $(this);
var $that = $this.closest('.container').find('.glyphicon');
if ($that.hasClass('.glyphicon-unchecked')) {alert('unchecked');}
else if ($that.hasClass('.glyphicon-check')) {alert('check');}
});
});
不需要在hasClass
函数中使用.
,因为它已经在寻找一个类。
试试这个:
if ($that.hasClass('glyphicon-unchecked')) {
alert('unchecked');
} else if ($that.hasClass('glyphicon-check')) {
alert('check');
}
<<p> JSFiddle演示/strong>