Why .click event handler is not updated here?
I am passing a table row to a function which is supposed to update the onclick attribute on an anchor.
Whatever I have tried I am not able to set it to a new value. I can see that it is jQuery selects the anchor. But when I alert before an after, onclick attribute still points to the same value not the 'url' parameter that is provided to the function:
function modifyTrEditUrl(tr, url) {
console.log(tr.find('td:eq(6) a')[0]);
tr.find('td:eq(6) a')
.attr('onclick', url); // I have tried
开发者_C百科 //.click(function(e){..}) way too!
console.log(tr.find('td:eq(6) a')[0]); //still the same value! :(
}
Any advices?
I'm not sure, but it sounds like you may be confusing the onClick attribute with the HREF attribute. When a link is clicked, the URL in the HREF attribute is used.
The onClick attribute is really used for triggering Javascript stuff. That said, if you're using jQuery, you should really stick to the bind
or live
functions (which click
is a wrapper for).
I'm not a jQuery (or even javascript) pro but I think from your code, you are trying to set the value of the onclick attribute to the url.
.attr('onclick', url);
I know that in a tag, the onclick attribute should be assigned a javascript function. I think the code is trying to create a tag like
<a onclick=url>
and I should url is a string object. Maybe that's why there are errors.
How about using
tr.find('td:eq(6) a').attr('href', url);
use .bind or .live , .live to late bind
精彩评论