【问题标题】:Iframe content not showingiframe 内容未显示
【发布时间】:2015-12-28 19:04:44
【问题描述】:

我有一个没有 src 的 iframe。这是因为 iframe 仅在用户单击按钮时显示网页内容(来自 src)(然后将 src 替换为网页 URL)。那个按钮已经在工作了。 但是当没有人点击按钮时,iframe 必须显示一些简单的默认文本而不是那个网页。在我的示例中,我编写了文本行“默认情况下必须显示此文本。”。为什么文字不显示? 我需要保留 onload javascript,因为这对 iframe 的功能很重要。

<iframe src="" class="prodframe pc" style='bottom:0px;position:absolute;width:90%;height:73%;margin:0px auto;left:0;right:0;border:none;background:none transparent;' frameborder='0' scrolling='auto' allowtransparency='true' onload='window.onmessage=function(e){var c=0;var o=document.getElementById("zoekframe");if(o.offsetParent){do{c += o.offsetTop;}while(o = o.offsetParent);};scrollTo(0,c);};' id=zoekframe>This text must be displayed by default.</iframe>

我在下面的链接上有一个小提琴:https://jsfiddle.net/mfhdwmya/

【问题讨论】:

  • 为了清楚起见:替换内容的按钮只是背景信息,您无需对此进行任何操作。唯一关心的是如何显示那个简单的文本行。

标签: html iframe


【解决方案1】:

这个问题和Alternate text for iframes非常相似

接受的解决方案是:

<script>
$(function () {

    $("iframe").not(":has([src])").each(function () {

    var ifrm = this;

    ifrm = (ifrm.contentWindow) ? ifrm.contentWindow : (ifrm.contentDocument.document) ? ifrm.contentDocument.document : ifrm.contentDocument;

    ifrm.document.open();
    ifrm.document.write($(this).attr("alt"));
    ifrm.document.close();

    });

});
</script>

我会发表评论,但我的声誉还不够。

【讨论】:

    猜你喜欢
    • 2017-10-09
    • 1970-01-01
    • 2014-08-04
    • 1970-01-01
    • 2012-09-26
    • 1970-01-01
    • 1970-01-01
    • 2013-11-20
    相关资源
    最近更新 更多