jQuery traversal.closest().find()不工作?(使用jsFiddle)
我希望使用jQuery遍历查找某个div,如果该div有特定的类,则显示一个警报 下面是HTML(外部div用于模板化):jQuery traversal.closest().find()不工作?(使用jsFiddle),jquery,html,traversal,Jquery,Html,Traversal,我希望使用jQuery遍历查找某个div,如果该div有特定的类,则显示一个警报 下面是HTML(外部div用于模板化): 无需在hasClass函数中使用,因为它已经在查找类 试试这个: 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');
}
更新了:)没有意识到您有多个。容器
,所以是的,您确实需要最近的
$(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');}
});
});
if ($that.hasClass('glyphicon-unchecked')) {
alert('unchecked');
} else if ($that.hasClass('glyphicon-check')) {
alert('check');
}