简体   繁体   English

jQuery在匿名函数定义中使用on()附加单击处理程序

[英]jQuery attach click handler with on() inside anonymous function definition

I try to attach a click handler for links with a specific class within an anonymous function definition which is unfortunately not working. 我尝试在匿名函数定义中为特定类的链接附加一个单击处理程序,这不幸地无法正常工作。 Why? 为什么?

(function($) {
  var init = function() {
    console.log('init...');
    mediaPlayer($('.media-toggle'));

    };
  var mediaPlayer = function(mediaLink) {
    console.log('media player init ...');
    console.log(mediaLink);
    mediaLink.on("click", function(e) {
        console.log('media toggle clicked ...');
        e.stopPropagation();
        e.preventDefault();
        alert('clicked');
      });
    };

  init();

})(jQuery);

The used HTML markup looks like: 使用的HTML标记如下所示:

<ul>
  <li>
    <a href="https://www.foo.org/audios/preview1.mp3" class="media-toggle">Preview 1</a>
    <audio src="https://www.foo.org/audios/preview1.mp3"></audio>
  </li>
  <li>
    <a href="https://www.foo.org/audios/preview2.mp3" class="media-toggle">Preview 2</a>
    <audio src="https://www.foo.org/audios/preview2.mp3"></audio>
  </li>
  <li>
    <a href="https://www.foo.org/audios/preview3.mp3" class="media-toggle">Preview 3</a>
    <audio src="https://www.foo.org/audios/preview3.mp3"></audio>
  </li>
</ul>

I have to admit, that I use the MediaElement.js library to play the audio files, which works fine. 我必须承认,我使用MediaElement.js库播放音频文件,效果很好。 If I would use a "global" function handler definition like the code snippet below, it would work. 如果我使用“全局”函数处理程序定义(如下面的代码片段),则它将起作用。 But I don't understand why and what is the difference. 但是我不明白为什么和有什么区别。

$(function(){
    $('.media-toggle').click(function(e) {
        e.preventDefault();
        alert('Clicked ...');
    });

I missed a very important part on my example. 我错过了我的榜样中非常重要的部分。 A piece of JS code after this function is or was responsible for the not working code. 此功能后的一段JS代码是导致无效代码的原因或负责原因。 Which removed all the event handlers by moving a parent div element (with all the links inside) to another part within the DOM tree. 通过将父div元素(包含所有链接在内)移到DOM树中的另一部分,从而删除了所有事件处理程序。 Instead of 代替

 $(".preview-audio").remove().insertAfter($(".foo")); 
I should rather have written 我应该写

 $(".preview-audio").insertAfter($(".foo")); 

The notable difference between the two pieces of code is that the one which doesn't work runs immediately and the one that does runs on DOM Ready. 这两段代码之间的显着区别是,不起作用的代码可以立即运行,而确实可以在DOM Ready上运行。

The problem is almost certainly that you are calling $('.media-toggle') before any elements with that class exist in the DOM, so it has a length of 0. 问题几乎可以肯定是,在DOM中存在具有该类的任何元素之前,您正在调用$('.media-toggle') ,因此它的长度为0。

Replace your IIFE: 更换您的IIFE:

(function($) {
...
})(jQuery);

with a ready handler: 使用现成的处理程序:

jQuery((function($) {
...
});

You have defined a function mediaPlayer but you have a typo when calling it: medialPlayer($('.media-toggle')); 您已经定义了一个函数mediaPlayer但是调用它时有一个错字: medialPlayer($('.media-toggle'));

It's likely you would see an error in the console. 您可能会在控制台中看到错误。

Also, make sure you've got this inside an onload or $(document).on('ready') function to ensure that the DOM node you're interested in ( .media-toggle ) has been loaded. 此外,请确保已将其放在onload$(document).on('ready')函数中,以确保已加载您感兴趣的DOM节点( .media-toggle )。

You are missing the doc ready code in here: 您在这里缺少文档就绪代码:

  init();// possibly element is not rendered.

})(jQuery);

Instead try this: 而是尝试以下方法:

  $(init);

})(jQuery);

Or: 要么:

(function($,document) {
  ....
  $(document).ready(init);

})(jQuery,document);

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

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