1

我对 jquery show and hide 有一个小问题。

我有一个按钮可以激活点击并显示一个框。

是否可以在框外单击以淡出框

这是我的代码。

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

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

3 回答 3

1

尝试这个:

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

$(document).not($(".normal-btn.interest")).click(function(){
    $('.categories-wrap').fadeOut();
});
于 2013-10-09T11:41:07.807 回答
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();
});
于 2013-10-09T11:33:38.663 回答
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>

它有效,你可以试试

于 2017-07-10T22:56:31.877 回答