这是我的代码:
<div id="main-content">
<script>
document.title = document.getElementById("main-content-iframe").contentDocument.title;
</script>
<iframe name="cstage" src="home.html" width="100%" height="100%" id="main-content-iframe" frameborder="0" onload="document.title=parent.frames['cframe'].document.title;">If you're seeing this message, it means IFrames are not supported; Please enable IFrames or upgrade to a compatible browser that supports IFrames.</iframe>
</div>
它不起作用,我做错了什么?
答案 0 :(得分:5)
您需要检查iframe是否已加载。
<div id="main-content">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<iframe name="cstage" src="home.html" width="100%" height="100%" id="main-content-iframe" frameborder="0" onload="document.title=parent.frames['cframe'].document.title;">If you're seeing this message, it means IFrames are not supported; Please enable IFrames or upgrade to a compatible browser that supports IFrames.</iframe>
<script>
$( "iframe" ).on('load',function() {
document.title = document.getElementById("main-content-iframe").contentDocument.title;
});
</script>
答案 1 :(得分:-1)
尝试代码也许可以帮到你
HTML code:
<div id="main-content">
<iframe name="cstage" src="home.html" width="100%" height="100%" id="main-content-iframe" frameborder="0">If you're seeing this message, it means IFrames are not supported; Please enable IFrames or upgrade to a compatible browser that supports IFrames.</iframe>
</div>
jQuery code:
alert($('#main-content-iframe').contents().find("title").text());
注意:确保home.html来自与主页相同的域。