比如我现在有一JSP页面,页面中有两个iframe   ID分别是  if1  if2
其中在if1 中有一文本框,ID="tx"
在if2 中有一按钮,请问,如果点周IF2中的按钮时,改变IF1中文本框的值

解决方案 »

  1.   

    if1 :
    var if2;
    if(frames.document.frames){
     if2=top.frames.document.frames("if2");
    }else{
    if2 =  top.document.getElementById('if2').contentWindow;
    }if2.document.getElementById('tx').value="";
      

  2.   

    具体参考...主文件<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>无标题文档</title>
    </head>
    <style type="text/css">
    *{font-family:verdana; font-size:12px;}
    div{border:1px solid red; padding:2px;}
    </style>
    <script type="text/javascript">
    function $(id){
    return "string" == typeof(id) ? document.getElementById(id) : id ;
    }</script>
    <body>
    <p>
    在 IFrame2 页面修改 IFrame1 页面的输入框值
    </p>
    IFrame1:<br>
    <iframe id="if1" name="if1" src="if1.htm" height="200" width="400"></iframe>
    <hr size="1">
    IFrame2:<br>
    <iframe id="if2" name="if2" src="if2.htm" height="200" width="400"></iframe>
    </body>
    </html>if1.htm<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>无标题文档</title>
    </head><body>
    <input type="text" id="tx" name="tx" value="input box" size="30">
    </body>
    </html>
    if2.htm<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>无标题文档</title>
    </head>
    <script type="text/javascript">
    function $(id){
    return "string" == typeof(id) ? document.getElementById(id) : id ;
    }function chanValue(){
    var s=$("input2").value;
    var ow=parent.document.getElementById("if1").contentWindow;
    var otx=ow.document.getElementById("tx");
    otx.value=s;
    }</script>
    <body>
    <input name="input2" type="text" id="input2" value="Test new value from iframe 2" size="30">
    <br>
    <input type="button" id="btn01" name="btn01" value="TEST" onClick="javascript:chanValue();">
    </body>
    </html>