2015-12-28 226 views
1

我有一个没有src的iframe。这是因为当用户单击按钮时(src被网页URL替换),iframe仅显示网页内容(来自src)。该按钮已经在工作。 但是,当没有人点击按钮时,iframe必须显示一些简单的默认文本,而不是该网页。在我的示例中,我写了文本行'这个文本必须默认显示'。为什么文字不显示? 我需要保持onload javascript,因为这对iframe的功能很重要。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/

+0

为清楚起见:替换内容的按钮只是背景信息,您不必对此做任何事情。唯一值得关注的是如何显示简单的文本行。 – Cinzel

回答

0

这个问题是非常相似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> 

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

+0

谢谢你,这个伎俩。 – Cinzel