如何更改孩子的文档标题?

时间:2011-11-04 17:03:07

标签: javascript html

它的设置如下:

Parent.html

<html>
<head>
<title>Component Documentation</title>
</head>
<frame src="html\child.html" name="child">
</html>

Child.html

<select name="componentselect" size="24">

单击选择框时,我需要更改父级的标题。基本上,我希望浏览器显示在“componentselect”中选择的项目以显示在标题栏中。在Child.html中设置document.title不起作用。

4 个答案:

答案 0 :(得分:1)

您可以使用top.parent.前缀来执行此操作。它就像您的window.前缀。 parent.将指向父级的window对象。 top.将指向主网站(如果使用多个级别的框架)。但在某些浏览器中,由于缺乏安全性,此功能已被禁用或限制。

所以这会奏效:

parent.document.title = "I came from outerspace!";

答案 1 :(得分:0)

可以做到这一点 Javascript函数:

<script>

    function changeSelect()
    {
        parent.document.title = this.options[this.selectedIndex];
    }
</script>

和html:

<select name="componentselect" onchange="changeSelect()" size="24">

尝试父母或者顶部,不记得使用

答案 2 :(得分:0)

function onSelectChange () {
  top.document.title = this.options[this.selectedIndex].text;
}

HTML

<select name="componentselect" onchange="onSelectChange()" size="24">

答案 3 :(得分:0)

我推荐一个Jquery解决方案,但是这不能用于框架,你可能想要考虑使用div,几乎总是使用框架的替代方案(它可能只需要更多的代码)。无论如何这里的脚本在自包含的html上确实有效:

<script>
$(document).ready(function($){
  $('#componentselect').click(function() {
    $(document).attr(’title’, $('#componentselect').val());
  });
}
</script>