0

In jQuery, can I bind a click event to a data handle instead of a class? I'm not sure how to word it, but this is what I'm trying to do...

<a data-user="jason">

$(data-user).click(function() { //This is just for example

Right now, I have it this way, but it's causing a lot of problems with other things. Can I use data instead of a class?

<a class="user">

$(".user").click(function() { //This is just for example
4

2 回答 2

4

使用属性等于选择器

$('a[data-user="jason"]').click(function() { .... }

OP评论后更新。

$('[data-user]').click(function() { .... }
于 2013-10-24T06:02:14.863 回答
1

尝试

$('a[data-user]').click(function() {});
于 2013-10-24T06:06:44.867 回答