2013-10-09 167 views
1

我有一個jquery顯示和隱藏的小問題。jquery顯示和隱藏按鈕

我有一個按鈕,激活點擊並顯示一個框。

是否可以單擊框外褪色的開箱

這裏是我的代碼。

$('.normal-btn.interest').click(function(){ 
    $('.categories-wrap').fadeIn(); 
}) 

$('what needs to be here? ').click(function(){ 
    $('.categories-wrap').fadeOut(); 
}) 
+0

用您的代碼發表提示 – Exception

回答

1

是的,你可以在click事件綁定到document,如:

$('.normal-btn.interest').click(function(e){ 

    // Prevent the event from bubbling up the DOM tree 
    e.stopPropagation(); 
    $('.categories-wrap').fadeIn(); 
}); 

$(document).click(function(){ 
    $('.categories-wrap').fadeOut(); 
}); 
+0

謝謝。但是,只要您單擊該框消失?? –

+0

事件冒泡,單擊按鈕時,顯示元素並在其後,第二個處理程序隱藏它。 – undefined

+0

@PaulDesigner:需要防止事件冒泡DOM樹。看看更新的代碼是否有幫助! –

1

試試這個:

$('.normal-btn.interest').click(function(e){ 
     e.stopPropagation(); 
    $('.categories-wrap').fadeIn(); 
}); 

$(document).not($(".normal-btn.interest")).click(function(){ 
    $('.categories-wrap').fadeOut(); 
}); 
0

我,我不喜歡這樣

<pre> 
<!DOCTYPE html> 
<html> 
<head> 
<script 
src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"> 
</script> 
<script> 
$(document).ready(function(){ 
$("#hide").click(function(){ 
    $("p").toggle(); 

    var x = $("#hide").text(); 

    if(x=="Hide"){ 
    $("button").html("show"); 

    } 
    else 
    { 
    $("button").html("Hide"); 

    } 

}); 

}); 

</script> 
</head> 
<body> 

<p>If you click on the "Hide" button, I will disappear.</p> 

<button id="hide">Hide</button> 



</body> 
</html> 

和它的工作你不能ry