【问题标题】:jQuery - accessing an IFrame buttonjQuery - 访问 IFrame 按钮
【发布时间】:2014-03-13 09:02:13
【问题描述】:

我的 HTML 页面有几个段落和一个对 IFrame 的引用,IFrame 包含一个表单,带有几个文本框、下拉列表和一个按钮。当用户单击 IFrame 上的按钮时,它会返回一条感谢消息。唯一的问题是 HTML 页面上的两个段落在显示感谢信息时仍然显示。单击 IFrame 上的按钮时,如何隐藏这两个段落? (我在一个 div 周围敲了两段)

谢谢

【问题讨论】:

    标签: javascript jquery html iframe


    【解决方案1】:

    在您的iframe script 中尝试此代码,

    $('button').on('click',function(){
       $('p').hide();
    });
    

    如果paragraph elementsparent window 中,则使用window.parent.document 之类的,

    $('button').on('click',function(){
       $('p', window.parent.document).hide();
    });
    

    【讨论】:

    • 段落不在 iframe 中,所以这段代码不起作用。
    【解决方案2】:

    好吧,我不得不猜测没有标记或 javascript,但是,假设主文档和 iframe 中显示的文档都在同一个域中,那么包含您的段落的 div 有一个 id 的 @ 987654322@ 并且您在 iframe 中的按钮的 id 为 formButton 您可以这样做(您什么也没说,我假设您没有使用 jQuery):

    在主文档脚本中:

    function hideParagraphs() {
        document.getElementById('pContainer').style.display = 'none';
    }
    

    在您的 iframe 中:

    window.onload = function() {
        document.getElementById('formButton').addEventListener('click', window.top.hideParagraphs, false);
    };
    

    ids 更改为适合您标记的那些。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-01
      • 1970-01-01
      • 2013-02-08
      • 2011-04-11
      • 2012-08-18
      相关资源
      最近更新 更多