Javascript:如何访问框架内的表

Javascript : How to access a table inside a frame?

本文关键字:框架 访问 何访问 Javascript      更新时间:2023-09-26

要访问ID的值,我们可以使用:document.getElementById(ID).value。但是我在访问表中的ID时遇到了一个问题。桌子里面有一个框架。框架的内容是一个表格,也是因为我想在表格上上下滚动。

main.html:

<script type="text/javascript">
 function show_name()
 {
   alert(document.getElementById("name1").value);
 }
</script>

<table id="table1">
 <tr>
   <td>
      <div id="mydiv">
        <!-- the ajax func call myframe.php and pu result here -->  
      </div>
  </td>
 </tr>
</table>
<input type="button" value="Show Name" onClick="show_name()">

myframe.php:

<iframe height="500px" height="400px" src="table2.php"></iframe>

下面是table2.php。它根据传递给它的参数以交互方式生成一个表。当然还有插入到"mydiv"中的反馈。

<?php
 // mysql_connect();
 // mysq_query....
?>
 <table id="table2">
 <tr>
  <td>
    <input type="text" id="name1" value="john">
  </td>
 </tr>
 </table>

代码运行得很好,所有内容都能正确地进入页面。我想用框架内的对象做一些事情,但如何访问它?

到目前为止,按下按钮没有任何结果,这意味着代码无法访问对象。当然,这将是一个更有意义的完整过程,而不仅仅是提醒一个名字。

谢谢你的帮助。

访问iframe:中的元素

window.frames[1].document.getElementById('name1');