2014-07-10 32 views
0

我期待找到使用jQuery橫移一定格,如果該div有一個特定的類,顯示一個警告.. http://jsfiddle.net/gfA4F/1/jQuery遍歷.closest()。find()不起作用? (帶的jsfiddle)

這裏是HTML(外格爲模板的目的):

<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');} 

    }); 

}); 

回答

2

無需在hasClass功能使用.,因爲它已經在尋找一類。

試試這個:

if ($that.hasClass('glyphicon-unchecked')) { 
    alert('unchecked'); 
} else if ($that.hasClass('glyphicon-check')) { 
    alert('check'); 
} 

JSFiddle Demo

+0

更新吧:)不知道你有多個'.container'所以,是的,你確實需要'closest' – imbondbaby

+1

真棒!很好,趕上,謝謝你 – StackThis