[英]Only slideToggle when parent is clicked
I'm having some problems with the slideToggle in JavaScript. 我在JavaScript中使用slideToggle时遇到了一些问题。 I have a class called .reveal and when I click on it, I want the class Window to appear. 我有一个名为.reveal的类,当我点击它时,我想要出现类Window。 However, I only want the slideToggle to work when I click on the parent class .reveal and not when I click inside the class .Window.. If I have a button inside my .Window class fx. 但是,我只希望slideToggle在我点击父类.reveal时工作,而不是当我在类中单击时.Window ..如果我在.Window类fx中有一个按钮。 the slidetoggle would kick in when I click it: 当我点击它时,slidetoggle会启动:
<div class="reveal">
Reveal
<div class="Window">
<input type="button" id="Create" value="CLICK" />
</div>
</div>
$(function () {
$('.reveal').click(function() {
$(this).children('.Window').slideToggle();
});
});
How do I make the slideToggle only work when I click the parent class? 单击父类时,如何使slideToggle仅起作用?
With your current html structure , that is how it is supposed to happen, clicking on .Window
will bubble up to .reveal
s click event since it is the parent, so either you need to stopPropagation()
by subscribing to a click event on the .Window
or check for the intented target alone in the existing click event, thus avoid another event registry. 使用您当前的html结构,就是它应该发生的方式,点击.Window
会冒泡到.reveal
的点击事件,因为它是父项,所以要么你需要通过订阅点击事件来.reveal
stopPropagation()
.Window
或在现有点击事件中单独检查目标目标,从而避免另一个事件注册表。 Instead try this: 而是试试这个:
$(function () {
$('.reveal').click(function(e) {
if(e.target == this) //check if target is .reveal only if so trigger else ignore.
$(this).children('.Window').slideToggle();
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.