middle click (new tabs) and javascript links

前端 未结 7 1900
温柔的废话
温柔的废话 2020-12-10 02:36

I am in charge of a website at work and recently I have added ajaxy requests to make it faster and more responsive. But it has raised an issue.

On my pages, there is

7条回答
  •  旧巷少年郎
    2020-12-10 02:51

    Yes. Instead of:

    ...
    

    Do this:

    ...
    

    And then in your JS, hook the link via it's ID to do the AJAX call. Remember that you need to stop the click event from bubbling up. Most frameworks have an event killer built in that you can call (just look at its Event class).

    Here's the event handling and event-killer in jquery:

    $("#thisLink").click(function(ev, ob) {
        alert("thisLink was clicked");
        ev.stopPropagation();
    });
    

    Of course you can be a lot more clever, while juggling things like this but I think it's important to stress that this method is so much cleaner than using onclick attributes.

    Keep your JS in the JS!

提交回复
热议问题