开发者

What's the jQuery way of writing this JavaScript?

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 + 
    "\"); 
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜