简体   繁体   中英

jQuery - grab href from click inside of an iframe

I have this code I got from a site. It works fine and it can detect a click inside of an iframe.

$('body', $('#iframeId1').contents()).click(function(event) {
 alert("Link clicked");
});

However, I am trying to get the href of the clicked link. I tried :

var href = $(this).attr("href");
  alert(href);

But this alerts undefined

If you want to bind all links (a tags) in your body, you want to do something like this. This will work as expected:

$('body').on('click', 'a', function(event) {
  alert("Link clicked");
  var href = $(this).attr("href");
  alert(href);
});

However, you won't be able to access or bind the contents of an iframe ( see this question ), since that would be a huge security problem.

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