简体   繁体   English

自定义jQuery选择器在我的情况下不起作用

[英]custom jquery selector doesn't work in my case

$('.modal-footer button').attr('data-print','').text('Print');

$(document).off('click', '.modal-footer button["data-print"]').on('click','.modal-footer button["data-print"]',function(){
   alert('fire');
});

I expect the code execute from top the bottom, so the custom selector should work, but it didn't, what's wrong there? 我希望代码从上到下执行,因此自定义选择器应该可以工作,但是没有,那是怎么回事?

You need to remove the quotes around the attribute name in the selector. 您需要在选择器中删除属性名称周围的引号。 Try this: 尝试这个:

$(document)
    .off('click', '.modal-footer button[data-print]')
    .on('click', '.modal-footer button[data-print]', function(){
        alert('fire');
    });

Example fiddle 小提琴的例子

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

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