【问题标题】:navigating to a link from inside iframe从 iframe 内部导航到链接
【发布时间】:2011-06-30 08:53:40
【问题描述】:

我希望这里有人能够提供帮助,我先谢谢了。

我有一个 iframe(在我的域中),当有人单击 iframe 中的链接时,我不希望该链接自动打开,我需要它转到一个功能,在添加一些内容后导航到该链接页。

我用这个函数来填充 iframe

function setIframeHtml(html) {

   var iframe = document.getElementById("myFrame");
   var doc = iframe.document;
   if (iframe.contentDocument) {
       doc = iframe.contentDocument; // For NS6
   }
   else if (iframe.contentWindow) {
       doc = iframe.contentWindow.document;  // For IE5.5 and IE6
   }
   // Put the content in the iframe
   doc.open();
   doc.writeln(html);
   doc.close();

   navigating = false;
}

函数获取的html参数是添加我的东西后要加载的页面。

当我从 iframe 内部执行此操作时,我收到一条错误消息

Cannot read property 'document' of null    

只有当我从外部调用该函数时,如果 iframe,它才起作用。

任何想法如何使它工作?

【问题讨论】:

    标签: javascript html iframe anchor


    【解决方案1】:

    我用的是jQuery,可能有不使用jQuery的方法,可惜我不知道。

    您可以指定代码在用户单击链接时运行,代码将运行,然后框架将转到链接指定的页面。

    这是一个带有警告框的示例。

    $("a").click(function() {
        //do whatever you need to do on a link press here.    
        alert("Test");
    });
    

    实际操作:http://jsfiddle.net/W2NMu/,请注意在关闭警报框之前框架不会改变。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-21
      • 1970-01-01
      • 2014-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多