将标题标记应用于iFrame中的帧

时间:2014-06-23 20:23:59

标签: javascript jquery html iframe

我正在尝试将标题标签添加到iframe中的框架。所有文件都驻留在同一个域中。

我在iframe中加载的html页面的完整选择器是“html> frameset:nth-​​child(2)> frame:nth-​​child(1)”

但是,即使使用以下代码也不会更新iframe中任何帧的属性。

$('#project_frame').contents().find('frame').attr('title','Table of Contents');

<iframe id="project_frame" name="project_frame" src="<?= $uri ?>" frameborder="0" title="project_frame"></iframe>

2 个答案:

答案 0 :(得分:1)

加载源的iframe与主执行是异步的,所以这是一个很好的技巧,可以使所需的代码等待直到它被加载:

<iframe id="project_frame" name="project_frame" src="<?= $uri ?>" frameborder="0" title="project_frame"></iframe>

$("#project_frame").load(function () {
    $("#project_frame").contents().find('frame:first').attr('title', 'Table of Contents');
});

在此处找到:Javascript callback when IFRAME is finished loading?

答案 1 :(得分:0)

document.getElementById("project_frame").contentWindow.document.title="MY TITLE";
相关问题