使用Javascript以编程方式单击iframe中的链接 [英] Programmatically click link in iframe with Javascript

查看:51
本文介绍了使用Javascript以编程方式单击iframe中的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的html/javascript.下面有两个我尝试过的if语句.我使用indexof来获取< a></a> 标记之间的文本.还尝试了 href == 使用 href = 的内容.两者都不起作用.

Here's my html/javascript. Below there are two if statements that I've tried. I used indexof to get the text between the <a></a> tags. Also tried href == to use the href= contents. Both do not work.

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>pb</title>
    <script type="text/javascript">
      function call() {
        alert("Called!");
        var allAnc = document.getElementById("d").contentDocument.getElementsByTagName("a");
        for (var i = 0; i < allAnc.length; i++) {
            if (allAnc[i].href.indexOf("Flag") > 0) {
                alert("Found link by indexof, Trying to click!");
                (allAnc[i]).click();
                break;
            }
            if (allAnc[i].href == "javascript:flag.closeit()") {
                alert("Found link by href, Trying to click!");
                (allAnc[i]).click();
                break;
            }
        }
      }
    </script>
  </head>
  <body>
    <input id="Button1" type="button" onclick="call()" value="button" />
    <iframe id="d" src="sourceurl.html" width="100%" height=700"></iframe>
  </body>
</html>

这是我要单击的iframe中的链接:

Here is the link inside the iframe I am trying to click:

<a style="text-align: left;" href="javascript:flag.closeit()" title="Flag">Flag</a>

推荐答案

根据您的评论,您正在将其他域的内容加载到iframe中,因此由于相同来源政策限制.

From your comment, you are loading the content of different domain into you iframe, so you just can't read the content of that domain due to same origin policy restrictions.

这篇关于使用Javascript以编程方式单击iframe中的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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