2012-08-29 131 views
0

我想在單擊按鈕時顯示隱藏範圍。 我的HTML看起來像:無法顯示隱藏範圍

<ul> 
    <li> 
     <span class="sfFormlabel" style="display:none" >show</span> 
     <div id="div1"> 
      <input type="button" value="show" class="show"> 
     </div> 
    </li> 
    <li> 
     <span class="sfFormlabel" style="display:none" >show1</span> 
     <div id="div2"> 
      <input type="button" value="show" class="show"> 
     </div> 
    </li> 
</ul> 

和jQuery:

$('.show').live("click", function() { 
    alert('test'); 
$(this).parent('li').children('span').show(); 
}); 

但我無法顯示隱藏的跨度。
jsfiddle

+0

我找到了這樣的解決方案。這是一個正確的方法來做到這一點.http://jsfiddle.net/shree/b43V7/2/ –

回答

1

您需要使用的.parent代替.parents.closest

$(this).parents('li').children('span').show(); 

或者

$(this).closest('li').children('span').show(); 

PS:.live是已被棄用,使用.on代替。

0

這裏有一個簡單的解決方案:

$(document).ready(function() { 
    $("#about").css("background-color","#222222"); 

    $('.show').live("click", function() { 
    alert('test'); 
$('.sfFormlabel').show(); 
}); 

}); 
+0

但是,如果你只希望其中的一個顯示,用xdazz答案,除非你給他們ids。 – NewInTheBusiness