简体   繁体   中英

How to detect the clicked href URL in JQuery/Javascript

Does anyone know how to detect the clicked href URL in JQuery? For example I have the following links, some is redirect to the new page and some will call to the javascript to expand the div. How can I use the JQuery/Javascript to detect the href if no ID been use.

<a href="www.test.com">Test 1</a>
<a href="javascript:test()">Test 2</a>
<a href="www.test2.com">Test 3</a>
<a href="www.test3.com">Test 4</a>
<a href="javascript:test2()">Test 5</a> 

Does anyone know how to detect the clicked href URL in JQuery?

$("a").click(function(event){
    // Capture the href from the selected link...
    var link = this.href;

    alert(link);

    // do something if required....

    // would prevent the link from executing, if that is something you want to do...
    return false; // not needed if you want the link to execute normally...
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM