Javascript 带有数据属性的 JQuery On Click
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13445929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JQuery On Click with data attribute
提问by Devin Dixon
Possible Duplicate:
Selecting element by data attribute
可能的重复:
按数据属性选择元素
I'm trying to listen to when an element with a certain data attribute is clicked but I can't seem to get the on click working and I'm sure its something easy on my part that I'm missing. I have
我正在尝试聆听具有特定数据属性的元素何时被点击,但我似乎无法让点击工作正常工作,而且我确信这对我来说很容易,但我错过了。我有
<a href="/home" data-spinner="true" />
$.data('record').click(function() {
//Do Action
});
I have that with variations. My question is, how can I use an data attribute with on click?
我有变化。我的问题是,如何在点击时使用数据属性?
回答by Robin Jonsson
Easy solution to your problem: (Not tested)
轻松解决您的问题:(未经测试)
$('a[data-spinner="true"]').click(function(event) {
});
回答by Akhil Sekharan
This selects all elements with the data-spinner attribute, regardless of the value of the attribute.
这将选择具有 data-spinner 属性的所有元素,而不管该属性的值如何。
$( "[data-spinner]" ).live( "click", function () {
console.log('clicked');
} );
回答by VisioN
The following code binds clickevent to all <a>elements which have data-spinnerattribute equal totrue:
以下代码将click事件绑定到属性等于 的所有<a>元素:data-spinnertrue
$("a[data-spinner='true']").click(function() {
//Do Acction
});

