I don't like the syntax onclick='javascript:doSomething(...
.
How could I write this in jQuery?
<a onclick='javascript:doSomething(\"" +
aName +
"\",\"" +
bName +
"\",\"" +
aLink +
"\",\"" +
aText +
"\",\"" +
aUrl +
"\");
return false;'
href='#'>
<img title='Do something' id开发者_JS百科='doSomethingIcon' src='/images/doSomething.png'>
</a>
You would use event handlers/bindings in jQuery:
$('a').click(function () {
doSomething(/* ... */);
return false; // prevent default behaviour (returning false implies
// event.preventDefault and event.stopPropagation)
});
.click(handler)
is a short way of saying .bind('click', handler)
. Have a look at the event docs for further details. The selector docs should help to find the right selector for your situation.
In jQuery you would use the click() method to assign a function to the onclick
event.
$("#doSomething").click(function(e){
doSomething('"'" + aName + '","' + bName + '","' + aLink + '","' + aText + '","' + aUrl + '"');
return false;
});
The $("#doSomething")
references the element with the ID of doSomething
Assume you have <a id="testID" href="#"><img...></a>
$('#testID').click(function(e) {
e.preventDefault(); // This is same as return; you have.
doSomething(\"" +
aName +
"\",\"" +
bName +
"\",\"" +
aLink +
"\",\"" +
aText +
"\",\"" +
aUrl +
"\");
});
精彩评论