简体   繁体   English

使用JavaScript获取iframe中的元素

[英]Get an element inside an iframe with javascript

how to get Element From an Iframe? 如何从iframe获取元素? i want when the attribute of an element that its in a frame is true; 我想要框架中元素的属性为true时; it show a text, and else show another text; 它显示一个文本,否则显示另一个文本; (sorry for bad english!) (对不起,英语不好!)

like that: 像那样:

<!DOCTYPE html>
    <html>

    <head>
    <title>document</title>
    </head>

    <body>

    <iframe id="frame" src="">

    <!--we think in frame there is a span tag with button id-->
    <span id="button" aria-passed="true">sss</span>
    </iframe>
    <br>

    <!--if its true-->
    <p id="content1" style="display:none;">
    true
    </p>
    <!--if its false-->
    <p id="content2" style="display:none;">
    false
    </p>

    <!--now we want that, if aria-passed=true show content1 else content2.-->
    <script type="text/javascript">
    var Frame = document.getElementById('frame');
    if(new RegExp ("true","gim").test(frame.contentWindow.document.getElementById('button').aria-passed="true") == true) {
     document.getElementById('content1').style.display="block";
    }
    else {
     document.getElementById('content2').style.display="block";
     }
    </script>
    </body>

    </html>

now i want to say why this code does not work???? 现在我想说为什么这个代码不起作用???? any ideas? 有任何想法吗?

It's much easier if you use jquery. 如果使用jquery,它会容易得多。

Example Code: 示例代码:

var elementattibutevalue = $("#frameID").contents().find("#htmlelementID").attr("SampleAttribute");

Hope it helps. 希望能帮助到你。 (^ _ ^) (^ _ ^)

I think the iframe in your code above is just a demonstration of how the loaded page code looks like? 我认为上面代码中的iframe只是演示了加载的页面代码的样子? Take a look at this post Javascript - Get element from within an iFrame . 看看这篇文章Javascript-从iFrame中获取元素 Also change your 'Frame' variable to lowercase. 还要将“ Frame”变量更改为小写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM