简体   繁体   English

添加的类不会触发其jQuery函数

[英]The added class doesn't trigger its jQuery function

It's an audio player: the idea is that the play button turns into a pause button (and viceversa) when clicked. 这是一个音频播放器:其想法是单击播放按钮时,它会变成暂停按钮(反之亦然)。

Thing is that the .pause event doesn't trigger the following function: 问题是.pause事件不会触发以下功能:

$('.pause').click(function(){
    player.pause();

    $(this).addClass('play');
    $(this).removeClass('pause');
});

The css shows that the pause class is set, but the function doesn't work. css显示已设置暂停类,但该功能不起作用。 Is there a way to make it work? 有办法使它起作用吗? (would be great to know why it didn't work) (很高兴知道为什么它不起作用)

jsFiddle jsFiddle

Use a delegated event binding to bind a handler that will be selector-aware without requiring rebinding on events. 使用委托事件绑定来绑定将支持选择器的处理程序,而无需重新绑定事件。

For the purposes of your demo, the selector would be along the lines of: 出于演示目的,选择器应遵循以下原则:

$('.player_controls').on('click', '.pause', function () {...});

Delegate event bindings attach the listener to a parent element that checks to see if the event fired was fired on an element that matches the provided selector. 委托事件绑定将侦听器附加到父元素,该父元素检查是否在与提供的选择器匹配的元素上触发了触发的事件。

jQuery docs jQuery文档

When a selector is provided, the event handler is referred to as delegated . 提供selector ,事件处理程序称为委托 The handler is not called when the event occurs directly on the bound element, but only for descendants (inner elements) that match the selector. 当事件直接发生在绑定元素上时,不调用处理程序,而仅对与选择器匹配的后代(内部元素)进行调用。 jQuery bubbles the event from the event target up to the element where the handler is attached (ie, innermost to outermost element) and runs the handler for any elements along that path matching the selector. jQuery使事件从事件目标一直冒泡到附加了处理程序的元素(即,最内层元素到最外层元素),并沿该路径运行与选择器匹配的任何元素的处理程序。

Event handlers are bound only to the currently selected elements; 事件处理程序仅绑定到当前选定的元素; they must exist on the page at the time your code makes the call to .on() . 它们必须在您的代码调用.on()时在页面上存在 To ensure the elements are present and can be selected, perform event binding inside a document ready handler for elements that are in the HTML markup on the page. 为了确保元素存在并可以选择,请在文档就绪处理程序内对页面上HTML标记中的元素执行事件绑定。 If new HTML is being injected into the page, select the elements and attach event handlers after the new HTML is placed into the page. 如果将新HTML注入到页面中,请将新HTML放入页面选择元素并附加事件处理程序。 Or, use delegated events to attach an event handler, as described next. 或者,使用委托事件来附加事件处理程序,如下所述。

Delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time. 委派事件的优势在于,它们可以处理来自后代元素的事件,这些事件在以后的时间添加到文档中。 By picking an element that is guaranteed to be present at the time the delegated event handler is attached, you can use delegated events to avoid the need to frequently attach and remove event handlers. 通过选择保证在附加委托事件处理程序时会出现的元素,您可以使用委托事件来避免频繁附加和删除事件处理程序的需要。 This element could be the container element of a view in a Model-View-Controller design, for example, or document if the event handler wants to monitor all bubbling events in the document. 此元素可以在一个模型-视图-控制器设计视图的容器元素,例如,或者document ,如果事件处理程序要监视文档中的所有冒泡事件。 The document element is available in the head of the document before loading any other HTML, so it is safe to attach events there without waiting for the document to be ready. 在加载任何其他HTML之前, document元素位于documenthead ,因此可以在其中附加事件,而不必等待文档准备就绪。

You can use event delegation for this. 您可以为此使用事件委托。 The issue is that binding directly (without delegation) binds to whichever elements exist at the time click is called. 问题在于,直接绑定(无委派)会绑定到调用click存在的任何元素。

$(".player_controls").on("click", ".pause", function(){

    /* ... */

});

Instead of using $('.pause').click(function(){...}) you would need to start using the $.on method to start listening for objects which are still not in the DOM. 而不是使用$('。pause')。click(function(){...}),您需要开始使用$ .on方法来开始侦听仍不在DOM中的对象。 eg 例如

$(".pause").parent().on("click",".pause", function(event){
    player.pause();
    $(this).addClass('play');
    $(this).removeClass('pause');
});

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

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