Apply “onclick” to all elements in an iFrame

前端 未结 2 1292
北恋
北恋 2021-01-17 08:07

How do I use the JavaScript DOM to apply onclick events to links inside of an iframe?

Here\'s what I\'m trying that isn\'t working:

2条回答
  •  醉梦人生
    2021-01-17 08:35

    getElementsByTagName returns a NodeCollection, so you have to iterate throgh this collection and add onclick handler to every node in that collection. The code below should work.

    var links = document.getElementById('myIframe').contentDocument.getElementsByTagName('a');
    for(var i=0;i

    also make sure, you run this code after the frames' content is loaded

    embed_results.onload=function(){
       // your code
    }
    

提交回复
热议问题