如何获取两个iframe中的元素属性 [英] How to get an attribute of element inside two iframes

查看:99
本文介绍了如何获取两个iframe中的元素属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<html>
  <head></head>
  <body>
    <iframe id="num1" src="/idk">
      <html>
        <head></head>
        <body>
          <iframe id="num2" src="/idk2">
            <html>
              <head></head>
              <body>
                <div id="div1">
                  <div id="div2">
                    <a href="http://www.blablabla.com"></a>
                  </div>
                </div>
              </body>
            </html>
          </iframe>
        </body>
      </html>
    </body>
    </html>

我的问题是 - 如何获得这两个iframe和两个iframe中的a标签的href属性div与javascript或jquery,我更喜欢JS。 (例如:http://www.blablabla.com)。

my question is - How do I get the href attribute of the a tag inside those two iframes and two divs with javascript or jquery, i prefer JS. (ex:"http://www.blablabla.com").

推荐答案

好吧,让第一个iframe很容易。只需掌握< iframe> 标签上的名称属性并使用 window.frames ['< name-of-iframe>'] 来获取它的窗口对象,然后可以用它来引用 document 该iFrame。期望 document.getElementById('num1')应该返回相同的结果是合理的,但后者返回html元素,而前者返回实际的窗口对象

Ok, so getting the first iframe is easy. Just slap on a name attribute on the <iframe> tag and use window.frames['<name-of-your-iframe>'] to get its window object that can then be used to reference the document of that iFrame. It seems reasonable to expect that document.getElementById('num1') should return the same thing, but the latter returns the html element, while the former returns the actual window object which is more useful for your purposes.

嵌套iFrame的问题在于,由于您要设置 src 属性,我认为你不会很容易获得访问权限。尽管你可以尝试使用这种方法。

The problem with the nested iFrame is that since you're setting the src attribute, I don't think you'll be able to get access so easily. Though you can try using this same method.

希望这有帮助。

这篇关于如何获取两个iframe中的元素属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆