这可能是我忽略的非常简单的事情,但我试图过滤" on"通过HTML5数据属性在jQuery中单击事件,但它没有被触发。
$(window).on('click', '[data-mydata]', function () {
// This never fires
});
$(window).on('click', function () {
$(this).attr('data-mydata'); // <-- but this is the right value
});
为什么不能过滤数据属性?我已经尝试过'[data-mydata]'
,':is([data-mydata])'
和':data(mydata)'
......而且我还在挠头。
在the documentation for the .on()
method中,他们会根据不同的元素类型进行过滤,但CSS3属性选择器似乎无法正常工作。
这是jQuery的1.11.2版本。
答案 0 :(得分:1)
你需要:
$(document).on('click', '[data-mydata]', function () {
而不是
$(window).on('click', '[data-mydata]', function () {