Select correct button when two buttons share the same class
I have the following HTML:
<a class="action_btn recommend_btn" act="recommend" href="recommend.php">
Recommend
</a>
and my jQuery is:
$(".action_btn").click(function() {
});
the p开发者_运维技巧roblem is that I have another
<a class="save action_btn" onclick="return false;">
Save
</a>
but I don't want this button to trigger this click function.
How can I do this?
Can't you use the other selector?
$(".recommend_btn").click(function() { /* code */});
Or you could filter the second one out (if you want to trigger that function on all .action_btn
elements, but not .save
elements:
$(".action_btn").not(".save").click(function() { /* code */});
You can select based on multiple conditions, so elements of class "action_btn" having an attribute "href" with a value of "recommend.php":
$('.action_btn[href="recommend.php"]').click(function(){})
you can do a specific element by adding an ID to that element since ID is unique to all.
you html would look like this
<a class="action_btn recommend_btn" id="recommentBtn" act="recommend" href="recommend.php">Recommend</a>
and jquery is
$("#recommentBtn").click(function() { /* code */});
or if you want class only you can do like this for you current html
$(".action_btn.recommend_btn").click(function() { /* code */});
the later will only look for the element with the both class.
To Select all action_btn except those with a save class use the not method:
$(".action_btn").not(".save").click(function() {
});
精彩评论