Adding onclick Attribute to <a> tag using JQuery
I have a link button that I want to change it onclick
callback function when clicked:
I am using .attr()
t开发者_运维知识库o change the onclick
event but it simply doesn't work.
$('#' + test).text('Unlike');
$('#' + test).attr("onclick", "return deleteLikeComment('"
+ CommentsWrapper + "','" + activityID + "', '" + test + "');");
But it simply doesn't work.
I just want to switch the callback method when the link is clicked.
Why are you attaching the event with an attr?
Why not just do like so:
$('#' + test).bind("click", function()
{
//functionality that you were trying to add using attr can now go inside here.
});
$('#' + test).click(function() {
return deleteLikeComment(...);
});
Use "click()" instead of attr()
If you want to switch the callback method every time the link is clicked, use .toggle()
. It alternates between the two provided functions.
精彩评论