简体   繁体   English

多个子对象上的jQuery stopPropagation()

[英]jQuery stopPropagation() on more than one child object

I have a list of songs, and when you click a song, a drop down menu appears and the song starts playing. 我有一个歌曲列表,当您单击一首歌曲时,会出现一个下拉菜单,歌曲开始播放。 I already have it setup so that if you click the drop down menu, it doesn't start pull itself back, you must still click on the original track details to pull it back up. 我已经设置好它,因此,如果您单击下拉菜单,它不会自动回弹,您仍然必须单击原始轨道详细信息才能将其回弹。

This example is here: http://shacktown.com 此示例在此处: http : //shacktown.com

I would also like to attach this functionality to a different button. 我还想将此功能附加到其他按钮上。 On a different page, though, I have the play/pause buttons right next to the song title, and I want to attach the stopPropagation() functionality to them as well. 但是,在不同的页面上,我在歌曲标题旁边有“播放/暂停”按钮,我也想将stopPropagation()功能附加到它们。

Here is that page: http://shacktown.com/oldstage.php 这是该页面: http : //shacktown.com/oldstage.php

The code that I'm using is here: 我正在使用的代码在这里:

    $(document).ready(function(){
        $('.track').click(function(){
            if ( $(this).find('.trackPlayer').is(':hidden') ) {
                //removing color from prev. track
                $(".selectedTrack").removeClass("selectedTrack");
                $('.trackPlayer').slideUp();
                $(this).addClass("selectedTrack");
                $(this).find('.trackPlayer').slideDown();

            } else {
                $(".selectedTrack").removeClass("selectedTrack");
                $(this).find('.trackPlayer').slideUp();
            }
        }).find('.trackPlayer').click(function(e) {
            e.stopPropagation();
        });
    });

My question is, how do I attach another one of these .find() / stopPropagation() functions without having to create another $(document).ready(function(){}) block? 我的问题是,如何在不创建另一个$(document).ready(function(){})块的情况下附加其中一个.find() / stopPropagation()函数?

Clicking the pause/play/stop button should not cause the menu to drop down either, I would like the buttons to be treated as if they were not inside the .track parent at all, if possible. 单击暂停/播放/停止按钮也不应导致菜单下拉,如果可能的话,我希望将按钮视为完全不在.track父级之内。

You can provide multiple CSS selectors. 您可以提供多个CSS选择器。 In your case looks like .trackInfo div contains play/pause buttons so the code will be 在您的情况下, .trackInfo div包含播放/暂停按钮,因此代码为

.find('.trackPlayer, .trackInfo').click(function(e) {
    e.stopPropagation();
});

Cool music, btw! 酷音乐,顺便说一句!

You can add a new click event within the same document.ready block: 您可以在同一document.ready块中添加新的click事件:

$(document).ready(function(){
    $('.track').click(function(){
        if ( $(this).find('.trackPlayer').is(':hidden') ) {
            //removing color from prev. track
            $(".selectedTrack").removeClass("selectedTrack");
            $('.trackPlayer').slideUp();
            $(this).addClass("selectedTrack");
            $(this).find('.trackPlayer').slideDown();

        } else {
            $(".selectedTrack").removeClass("selectedTrack");
            $(this).find('.trackPlayer').slideUp();
        }
    }).find('.trackPlayer').click(function(e) {
        e.stopPropagation();
    });

    $('.mybutton').click(function(){
        // do stuff here
    });
});

You don't have to chain all your calls together. 您不必将所有电话连在一起。 Just separate them out and make another call to find on your .track object. 只需将它们分开,然后再次调用即可在您的.track对象上find

$(document).ready(function(){
    var track = $('.track');

    track.click(function(){
        if ( $(this).find('.trackPlayer').is(':hidden') ) {
            //removing color from prev. track
            $(".selectedTrack").removeClass("selectedTrack");
            $('.trackPlayer').slideUp();
            $(this).addClass("selectedTrack");
            $(this).find('.trackPlayer').slideDown();

        } else {
            $(".selectedTrack").removeClass("selectedTrack");
            $(this).find('.trackPlayer').slideUp();
        }
    });

    track.find('.trackPlayer').click(function(e) {
        e.stopPropagation();
    });

    track.find('.somethingElse').click(function(e) {
        e.stopPropagation();
    });
});

If you want to keep the chaining, use end to return to the previous context. 如果要保留链接,请使用end返回上一个上下文。

$(document).ready(function(){
    $('.track').click(function(){
        if ( $(this).find('.trackPlayer').is(':hidden') ) {
            //removing color from prev. track
            $(".selectedTrack").removeClass("selectedTrack");
            $('.trackPlayer').slideUp();
            $(this).addClass("selectedTrack");
            $(this).find('.trackPlayer').slideDown();

        } else {
            $(".selectedTrack").removeClass("selectedTrack");
            $(this).find('.trackPlayer').slideUp();
        }
    })
    .find('.trackPlayer').click(function(e) {
        e.stopPropagation();
    })
    .end()
    .find('.somethingElse').click(function(e) {
        e.stopPropagation();
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM