如何从index.html上设置子iframe中的<title>

时间:2017-12-08 13:23:24

标签: javascript html iframe title

&lt; p&gt;目前,我的&lt; code&gt; index.html&lt; / code&gt;中包含以下内容:文件&LT; / p为H. &lt; pre&gt;&lt; code&gt; document.title = document.getElementById(&#39; title&#39;)。contentWindow.document.title; &LT; /代码&GT;&LT; /预&GT; &lt; p&gt;但它使标题空白而不是iframe的标题。&lt; / p&gt; &lt; p&gt;如何解决此问题?&lt; / p&gt; &lt; p&gt;&lt; a href =&#34; http://deepmotions.github.io" rel =&#34; nofollow noreferrer&#34;&gt;这是我的网站&lt; / a&gt;&lt; / p&gt;

1 个答案:

答案 0 :(得分:1)

获取title的{​​{1}}非常简单:

iframe

如果要在加载var iframe = document.getElementById("iframeId"); var iframeTitle = iframe.contentDocument.title; 时获取title的值,请执行:

iframe

document.getElementById("iframeId").onload = function() { var iframeTitle = document.getElementById("iframeId").contentDocument.title; } 值现已存储在title变量中。

之后,您可以使用此变量替换当前页面上的标题值。

iframeTitle

如果您要将此代码插入html文件,请在if (document.title != iframeTitle) { document.title = iframeTitle; } 元素中的内容后添加以下代码。

<body>