开发者

quick jquery href question

开发者 https://www.devze.com 2023-02-18 15:36 出处:网络
I have an anchor <a href=\"javascript:__doPostBack...\">1</a> I want to remove the anchor and replace it with an onClick jquery event handler like this:

I have an anchor <a href="javascript:__doPostBack...">1</a>

I want to remove the anchor and replace it with an onClick jquery event handler like this:

$(myselector).click(function () { ... });

I'm wondering what to p开发者_StackOverflowut in the ... since this is a javascript call.

Thanks for suggestions.


$(myselector).click(function(){ ...; return false });


You still need an href in your markup otherwise it won't be clickable - # is the normal value:

<a href="#" id="myId">1</a>

And in your Javascript:

$('#myId').click(function () {
    __doPostBack...
    return false;
});

You need the return false at the end otherwise your browser will still try to follow the href.


If your selector matches the anchor itself, then you can just use the .click() method and ensure it returns false, that will prevent the default execution of the href value.

You could remove the anchor entirely by using .unwrap().


you would do:

$(myselector).click(function () { __doPostBack... });

and if the $(myselector) was still an anchor tag you would also need a return false;


return false; probably does the trick, but you should be using preventDefault...

$(myselector).click(function(e) {
  // ...
  e.preventDefault();
 });
0

精彩评论

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