How to detect a link click with custom attributes with jQuery?
I have some links like this:
<a href="#" track="yes">开发者_Python百科My Link</a>
How can I detect when a link with the track attribute is clicked ?
Thanks!
Use the attribute selector:
$("a[track]").click(function(e){
// Your code
});
Example: http://jsfiddle.net/jonathon/uXwSF/
As andre points out in the comments, if you want to get only links where track='yes'
then do:
$("a[track='yes']").click(function(e){
// Your code
});
If you want to get all links with the track
attribute, but know what the value is then:
$("a[track]").click(function(e){
var shouldTrack = $(this).attr('track');
});
$("a[track]").click(function()
{
...
});
This will bind a click event to every link with a track
attribute.
An even better solution is to use live to limit the number of event handler:
$("a[track]").live("click", function()
{
...
});
精彩评论