简体   繁体   中英

get url of clicked href

How can I get url of clicked anchor text everywhere on the page. I tried this but it only works for "cls" class but not for any anchor.

$('.cls').click(function() { 
//here I need to get href url
});

For any anchor in your page you can simply do

$('a').click(function () {
    alert($(this).attr('href'));
});

This will give you url of clicked anchor text everywhere on the page.

For more details checkout:-

Try to use .attr('attributeName') to retrieve the clicked element's attribute,

$('.cls').click(function(e) {
  e.preventDefault();
  console.log($(this).attr('href'));
});

You can use attr() as below:

$(function(){
    $('a').click(function(){
    alert($(this).attr('href'));
});

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