[英]Show the specified element when only the specified link is clicked
I have this html structure 我有这个HTML结构
<a href="#" class="dp">Click me</a>
<div class="dp_div" style="display: none;">
this is the div content
</div>
I want to show the hidden div that has a class of "dp_div" when click on a link that has a class of "dp" and then if click to other element (if the current target click element is not '.dp' and '.dp_div'), the .dp_div will be hide, on the contrary, when click either .dp_div or the .dp then do not hide. 我想在单击具有“ dp”类的链接,然后单击到其他元素(如果当前目标click元素不是“ .dp”和“ .dp_div'),则.dp_div将被隐藏,相反,当单击.dp_div或.dp时,则不隐藏。
This is what I tried so far but sadly not working. 到目前为止,这是我尝试过的方法,但遗憾的是无法正常工作。
$(document).on('click', function(event) {
if ($(event.target).hasClass("dp") || $(event.target).hasClass("dp_div"))
{
alert('either .dp or .dp_div is click!!');
} else {
alert("not .dp or .dp_div is click");
}
});
You code snippet is missing a paranthesis 您的代码段缺少一个括号
$(document).on('click', function(event) {
if ($(event.target).hasClass("dp") || $(event.target).hasClass("dp_div"))
{
alert('either .dp or .dp_div is click!!');
} else {
alert("not .dp or .dp_div is click");
}
});
Please see the fiddle here for a demo 请在这里查看小提琴以进行演示
This code will show the element if the HREF
(link) is clicked on: 如果单击HREF
(链接),则此代码将显示该元素:
$(document).on('click', function(event) {
if ($(event.target).hasClass("dp") || $(event.target).hasClass("dp_div")){
$('.dp_div').fadeIn();
} else {
alert("not .dp or .dp_div is click");
}
});
http://jsfiddle.net/L0LmLquj/ http://jsfiddle.net/L0LmLquj/
You may use: 您可以使用:
$('.dp_div').show();
or 要么
$('.dp_div').fadeIn();
or even: $('.dp_div').toggle();
甚至: $('.dp_div').toggle();
if you wish to toggle the element upon clickation. 如果您希望在单击时切换元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.