I am using this code:
我正在使用此代码:
$(document).ready(function(){
$(".SlideDiv").hide();
$(".Show_hide").show();
$('.Show_hide').click(function(){
$(".SlideDiv").slideToggle();
});
});
And this HTML:
这个HTML:
<a href="#" class="Show_hide">Show/hide</a>
<div class="SlideDiv">this is content page
<a class="agree" href="https://www.google.com/" target="_blank">google</a>
</div>
And this CSS:
这个CSS:
.SlideDiv
{
height:200px;
background-color:gray;
margin-top:10px;
}
.Show_hide
{
display:none;
}
The problem is that I have links inside the DIV and when they click that link open but show function didn't hide.
问题是我在DIV中有链接,当他们点击该链接打开但show功能没有隐藏。
演示
2 个解决方案
#1
0
DEMO
No need to use any plugins for this. You just need to bind every click on the document to a function and check if the click was on an object is not any of the functional links. event.stopPropagation()
is used to prevent bubbling of events.
不需要使用任何插件。您只需将文档上的每次单击绑定到一个函数,并检查对象上的单击是否不是任何功能链接。 event.stopPropagation()用于防止事件冒泡。
$(document).ready(function(){
$(document).on('click',function(e){
if( !$(e.target).is('.Show_hide, .SlideDiv') ||
!$(e.target).parents('.Show_hide, .SlideDiv').length == 0){
$(".SlideDiv").slideUp();
e.stopPropagation();
}
});
$(".SlideDiv").hide();
$(".Show_hide").show();
$('.Show_hide').click(function(){
$(".SlideDiv").slideToggle();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<a href="#" class="Show_hide">Show/hide</a>
<div class="SlideDiv">this is content page
<a class="agree" href="https://www.google.com/" target="_blank">google</a>
</div>
#2
1
you can do this with a js "mousedownoutside.js" using following code
您可以使用以下代码使用js“mousedownoutside.js”执行此操作
http://benalman.com/projects/jquery-outside-events-plugin/
http://benalman.com/projects/jquery-outside-events-plugin/
$('yourDiv').on('mousedownoutside', function(event){
var target = $( event.target );
if($(this) != target){
$(this).slideUp();
}
});
#1
0
DEMO
No need to use any plugins for this. You just need to bind every click on the document to a function and check if the click was on an object is not any of the functional links. event.stopPropagation()
is used to prevent bubbling of events.
不需要使用任何插件。您只需将文档上的每次单击绑定到一个函数,并检查对象上的单击是否不是任何功能链接。 event.stopPropagation()用于防止事件冒泡。
$(document).ready(function(){
$(document).on('click',function(e){
if( !$(e.target).is('.Show_hide, .SlideDiv') ||
!$(e.target).parents('.Show_hide, .SlideDiv').length == 0){
$(".SlideDiv").slideUp();
e.stopPropagation();
}
});
$(".SlideDiv").hide();
$(".Show_hide").show();
$('.Show_hide').click(function(){
$(".SlideDiv").slideToggle();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<a href="#" class="Show_hide">Show/hide</a>
<div class="SlideDiv">this is content page
<a class="agree" href="https://www.google.com/" target="_blank">google</a>
</div>
#2
1
you can do this with a js "mousedownoutside.js" using following code
您可以使用以下代码使用js“mousedownoutside.js”执行此操作
http://benalman.com/projects/jquery-outside-events-plugin/
http://benalman.com/projects/jquery-outside-events-plugin/
$('yourDiv').on('mousedownoutside', function(event){
var target = $( event.target );
if($(this) != target){
$(this).slideUp();
}
});