jQuery 在单击主包装器时显示元素,但在单击特定元素时不显示

发布于 2024-12-29 12:02:49 字数 1707 浏览 0 评论 0原文

我在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瀟灑尐姊 2025-01-05 12:02:49

正如您所说,您不必使用 live,直接将单击事件处理程序附加到两个元素并调用 event.stopPropagation() [docs] 防止事件冒泡并触发其他事件处理程序:

$('.content, #emailThis').click(function(e) {
    e.stopPropagation();
    $(this).children().slideToggle('slow');
});

如果您无法使用事件处理程序,例如为此,我建议您使用通用类,将元素与其必须切换的子元素关联起来。例如,应该具有这种行为的元素可以具有类 header 和应该切换的子元素 toggle (抱歉,我在moment):

$('.header').click(function(e) {
    e.stopPropagation();
    $(this).children('.toggle').slideToggle('slow');
});

否则,您必须为几个元素重复基本相同的行为,这会降低代码的可维护性。

As you said you don't have to use live, directly attach the click event handler to both elements and call event.stopPropagation() [docs] to prevent the event from bubbling up and triggering other event handlers:

$('.content, #emailThis').click(function(e) {
    e.stopPropagation();
    $(this).children().slideToggle('slow');
});

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 toggled toggle (sorry, I'm a bit uncreative at the moment):

$('.header').click(function(e) {
    e.stopPropagation();
    $(this).children('.toggle').slideToggle('slow');
});

Otherwise you have to duplicate basically the same behaviour for a couple of elements, which reduces maintainability of your code.

蘑菇王子 2025-01-05 12:02:49

#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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文