jQuery 在单击主包装器时显示元素,但在单击特定元素时不显示
我在使用 jQuery 进行简单的菜单隐藏/显示时遇到了麻烦。
HTML 标记如下:
<div class="content borderRad5" id="mainWrapper">
<div id="ticketControl" class="noPrint blackBoxShadow">
<span id="printThis" class="TicketButton">Print</span>
<span id="emailThis" class="TicketButton">Email as Word</span>
<div id="emailDoc" class="blackBoxShadow borderRad5">
<div class="underline">Email to:</div>
<label>Email</label>
<span>
<input type="text" class="smallText" />
</span>
</div>
<span id="wordThis" class="TicketButton">Save as Word</span>
<span id="addToLoyalty" class="TicketButton">Add to Account</span>
<span id="addNote" class="TicketButton">Add Note</span>
<span id="refundThis" class="TicketButton">Refund</span>
</div>
---- THE REST OF THE PAGE ----
</div>
#ticketControl
是一个包含菜单的隐藏 div。当鼠标单击 .content
div 内的任意位置时,我希望它滑入视图中。下面的 jQuery 确实做到了这一点。
$('.content').live('click', function () {
$('#ticketControl').slideToggle('slow');
});
但是,我在这个主菜单 #emailDoc
中有第二个隐藏的 div,当单击 #emailThis
时需要滑入视图,显然这很容易,但是,我可以'不要阻止它再次隐藏菜单。
我意识到它为什么这样做,因为 .content
中的任何内容都会触发上面的 SlideToggle 函数,但我似乎无法阻止它,我尝试使用 .not('.TicketButton ')
并且还尝试获取父 ID 并排除它们等等,但我显然错过了一些明显的东西!
非常感谢任何帮助(是的,我意识到我应该使用 jquery on
但我现在坚持使用 live
...!)
I'm having troubles with a simple menu hide/show with jQuery.
The HTML markup is as follows:
<div class="content borderRad5" id="mainWrapper">
<div id="ticketControl" class="noPrint blackBoxShadow">
<span id="printThis" class="TicketButton">Print</span>
<span id="emailThis" class="TicketButton">Email as Word</span>
<div id="emailDoc" class="blackBoxShadow borderRad5">
<div class="underline">Email to:</div>
<label>Email</label>
<span>
<input type="text" class="smallText" />
</span>
</div>
<span id="wordThis" class="TicketButton">Save as Word</span>
<span id="addToLoyalty" class="TicketButton">Add to Account</span>
<span id="addNote" class="TicketButton">Add Note</span>
<span id="refundThis" class="TicketButton">Refund</span>
</div>
---- THE REST OF THE PAGE ----
</div>
#ticketControl
is a hidden div containing the menu. When the mouse is clicked anywhere within the .content
div, I want it to slide into view. The jQuery below does this just lovely.
$('.content').live('click', function () {
$('#ticketControl').slideToggle('slow');
});
However, I have a second hidden div within this main menu #emailDoc
which needs to slide into view when #emailThis
is clicked, obviously this is easy, BUT, I can't stop it from hiding the menu again.
I realise why it's doing it, because anything within .content
triggers the slideToggle function as above, but I can't seem to stop it, I've tried using .not('.TicketButton')
and also trying to get parent IDs and excluding them and so on, but I'm clearly missing something obvious!
Any help much appreciated (and yes, I realise I should be using jquery on
but I'm sticking with live
for now...!)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如您所说,您不必使用
live
,直接将单击事件处理程序附加到两个元素并调用event.stopPropagation()
[docs] 防止事件冒泡并触发其他事件处理程序:如果您无法使用事件处理程序,例如为此,我建议您使用通用类,将元素与其必须切换的子元素关联起来。例如,应该具有这种行为的元素可以具有类
header
和应该切换的子元素toggle
(抱歉,我在moment):否则,您必须为几个元素重复基本相同的行为,这会降低代码的可维护性。
As you said you don't have to use
live
, directly attach the click event handler to both elements and callevent.stopPropagation()
[docs] to prevent the event from bubbling up and triggering other event handlers:If you cannot use the event handler like this, I suggest you use generic classes which associate elements with their children which have to toggled. For example the elements which should have such a behaviour could have the class
header
and the children which are supposed to be toggledtoggle
(sorry, I'm a bit uncreative at the moment):Otherwise you have to duplicate basically the same behaviour for a couple of elements, which reduces maintainability of your code.
在
#emailThis
的点击回调函数中调用event.stopPropagation()
,以便事件不会将 DOM 冒泡到父元素。Call
event.stopPropagation()
in the click callback function for#emailThis
so that the event doesn't bubble up the DOM to the parent elements.