简体   繁体   English

具有类选择器的jQuery click事件仅触发一次

[英]jQuery click event with class selector only fires once

I'm using classed links to change FlowPlayer content. 我正在使用分类链接来更改FlowPlayer的内容。 Here is a working version: http://jsfiddle.net/r9fAj/ 这是一个工作版本: http : //jsfiddle.net/r9fAj/

In my actual page using the same code the first link clicked works fine. 在我使用相同代码的实际页面中,单击的第一个链接可以正常工作。 The second one does not fire the click function at all. 第二个根本不触发点击功能。 Even if I comment out everything but the console.log()... 即使我注释掉了console.log()之外的所有内容...

$('.playerLink').click( function() {
    audioPlayer.unload();
    initAudioPlayer();
    $('#player').css('display', 'block');
    $('#player').animate({"height":"50px"}, 1000);
    var newClip = {'url':$(this).attr('ajax-data'),'autoplay':true};
    audioPlayer.play(newClip);
    console.log('playing ' + $(this).attr('ajax-data'));
});

HTML like so 像这样的HTML

<a href="#" ajax-data="/audio/episodes/09_27_2013_Happy_Hour_88509726.mp3" class="playerLink">Listen</a>
<a href="#" ajax-data="/audio/episodes/10_04_2013_Happy_Hour_3478965.mp3" class="playerLink">Listen</a>

<a id="flowplayer" href="/audio/episodes/09_27_2013_Happy_Hour_88509726.mp3"></a>

And the player initialized like so: 播放器初始化如下:

var audioPlayer;

var initAudioPlayer = function () {
    $f("flowplayer", "/player/flowplayer-3.2.16.swf", {
        plugins: {
            controls: {
                fullscreen: false,
                autoHide: false,
            }
        },
        clip: {
            autoPlay: false,
            url: "",
        }
    });

    audioPlayer = $f();
};
initAudioPlayer();

Since the jsFiddle works over and over I assume something else in my page is preventing the second click() from working but the console has no errors for me. 由于jsFiddle一遍又一遍地工作,因此我认为页面中的其他内容正在阻止第二个click()起作用,但是控制台对我来说没有任何错误。

So my question is, short of posting the whole site's code how do I pursue debugging this? 所以我的问题是,如果没有发布整个站点的代码,我将如何进行调试?

So it sounds like your .click() event handler is only being fired for the first link you click and not for additional clicks. 因此,听起来您的.click()事件处理程序仅针对您单击的第一个链接而没有其他点击而被触发。 For general debugging, you could take your page that is not working and gradually comment out / remove other part of the JS and HTML until you are able to make it work correctly. 对于常规调试,您可以将无法使用的页面移开并逐步注释掉/删除JS和HTML的其他部分,直到能够使其正常工作为止。 Or start with the minimal amount that is working (the fiddle) and gradually add in the rest to see when it stops working. 或者从最小的可用数量开始(小提琴),然后逐渐添加其余数量以查看何时停止工作。

So this is the first site I have done where content is delivered via AJAX and internal links are caught by 所以这是我做的第一个站点,通过AJAX传递内容并捕获内部链接

 $("a:not([href^='http://'])").click( function(e) { 
    var url = $(this).attr("href");
    var title = ($(this).attr("title")) ? ': ' + $(this).attr("title") : '';
    e.preventDefault(); 
    if(url!=window.location){
       window.history.pushState({path:url},title,url);
       $('#contentMain').load(url);
        document.title = "It's New Orleans" + title;   
    }
});

For some reason it does work once to click a link with the class but the second time gets preventDefault()ed. 出于某种原因,单击类的链接确实可以工作一次,但是第二次获得preventDefault()ed。

<a href="#" ajax-data="/audio/foo.mp3" class="playerLink">Listen</a>

The fix was adding [href^='#'] to not() eg 该修复程序将[href^='#']到not()中,例如

$("a:not([href^='http://'],[href^='#'])").click( function(e) {

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

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