如何在没有类或ID的情况下获取iFrame中的链接

时间:2022-12-07 21:12:15

I want to find a link inside an iframe using jquery. This example of his scripts:

我想使用jquery在iframe中找到一个链接。这个脚本的例子:

<body>
    <iframe src='content.html' framborder='0'>
        <html>
            <body>
                <div class="afk">
                    <a href='index.html' target='_blank'>
                        <p>Text Here</p>
                    </a>
                </div>
            </body>
        </html>
    </iframe>

    <iframe src='content.html' framborder='0'>
        <html>
            <body>
                <div class="afk">
                    <a href='index-2.html' target='_blank'>
                        <p>Text Here</p>
                    </a>
                </div>
            </body>
        </html>
    </iframe>
</body>

how do I get the value of the "href" of the link inside the iframe?

如何获取iframe内链接的“href”值?

2 个解决方案

#1


0  

You can use

您可以使用

    //select frames
    var iframes = jQuery('iframe');

    $.each(iframes, function(i, iframe){
//this gives access to the window object of iframe
     childWindow = iframe.contentWindow;
      //now fire your selector query here
     console.log(childWindow.document.querySelector("div a").href); 
//or console.log(childWindow document.querySelector("div a").getAttribute("href"));
    })

#2


0  

First you must wait a iframe is loaded, and if the iframe src have permissions, you can travel the dom of your iframe like as another page dom.

首先,您必须等待iframe加载,如果iframe src具有权限,您可以将iframe的dom移动到另一个页面dom。

$('#myiframeid').load(function(){
  $(this).contents().find('a').each(function(){
    console.log($(this).attr('href'));
  });
});

#1


0  

You can use

您可以使用

    //select frames
    var iframes = jQuery('iframe');

    $.each(iframes, function(i, iframe){
//this gives access to the window object of iframe
     childWindow = iframe.contentWindow;
      //now fire your selector query here
     console.log(childWindow.document.querySelector("div a").href); 
//or console.log(childWindow document.querySelector("div a").getAttribute("href"));
    })

#2


0  

First you must wait a iframe is loaded, and if the iframe src have permissions, you can travel the dom of your iframe like as another page dom.

首先,您必须等待iframe加载,如果iframe src具有权限,您可以将iframe的dom移动到另一个页面dom。

$('#myiframeid').load(function(){
  $(this).contents().find('a').each(function(){
    console.log($(this).attr('href'));
  });
});