【问题标题】:how to change the src of the frame dynamically如何动态更改框架的src
【发布时间】:2013-06-03 11:20:25
【问题描述】:

我有一个“cars.html”HTML 文件,在一个框架集中包含三个框架

<frameset id="help" rows="10%,5%,90%">

<frame id="pageTitle" frameborder=0 scrolling=no src="cars_pageTitle.html">
<frame id="pageMenu" frameborder=0 marginheight="0" src="cars_pageMenu.html">
<frame id="pageContent" frameborder=0 marginheight="0" src="#">

 </frameset>

“cars_pageMenu.html”有菜单栏
我把这个分配给第二帧的src

<script type="text/javascript">
function carRequestMenu()
{
     alert(document.getElementById("pageContent"));
document.getElementById("pageContent").src='cars_requestMenuContent.html';
}
</script>

<BODY>
 <div id="pageToolbar" style="width=100%;height:10px;background-color:FF3333">
      <menu type="toolbar" style="margin-left: 0px;margin-bottom: 0px">
       <button type="button" onclick="carRequestMenu()">Request a Car</button>
       </menu>
 </div>
</BODY>

当我单击“请求汽车”按钮时,会调用 carRequestMenu() 函数 警报显示空值。我无法动态更改 src。 谢谢

【问题讨论】:

  • NULL 表示它没有得到任何东西。检查控制台您遇到了什么错误
  • 控制台也说是null

标签: javascript html dom frames


【解决方案1】:

您可以使用 window.open 并指定目标名称:

    <frameset id="help" rows="10%,5%,90%">

<frame id="pageTitle" frameborder=0 scrolling=no src="cars_pageTitle.html">
<frame id="pageMenu" frameborder=0 marginheight="0" src="cars_pageMenu.html">
<frame id="pageContent" name="pageContent" frameborder=0 marginheight="0" src="#">

 </frameset>


<script type="text/javascript">
function carRequestMenu()
{
     window.open('cars_requestMenuContent.html','pageContent');
}
</script>

<BODY>
 <div id="pageToolbar" style="width=100%;height:10px;background-color:FF3333">
      <menu type="toolbar" style="margin-left: 0px;margin-bottom: 0px">
       <button type="button" onclick="carRequestMenu()">Request a Car</button>
       </menu>
 </div>
</BODY>

【讨论】:

  • 您好 Alex Wang,它正在打开一个新窗口。但我希望更改发生在同一页面中
  • 我解决了问题,而不是 id 我使用 name 并遵循 alex 的评论
猜你喜欢
  • 1970-01-01
  • 2012-01-26
  • 2018-09-15
  • 1970-01-01
  • 1970-01-01
  • 2021-05-31
  • 2012-02-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多