你例子中的form不是有名字吗?

解决方案 »

  1.   

    <script>
    function f(obj){
    alert(obj.parentElement.name);
    }
    </script>
    <form name="f1">
    <a href="#" onClick="f(this)">1</a>
    </form>
    <form name="f2">
    <a href="#" onClick="f(this)">2</a>
    </form>
    <form name="f3">
    <a href="#" onClick="f(this)">3</a>
    </form>
      

  2.   

    <script>
    function f(obj){
        var theForms=document.forms;
        if(theForms==null) return null;
        for(var i=0;i<theForms.length;i++)
        {
         if(theForms[i].contains(obj))
         return theForms[i];  
        }
        return null;
    }
    </script>
    <form name="f1">
    <a href="#" onClick="alert(f(this).name);">1</a>
    </form>      
    <form name="f2">
    <a href="#" onClick="alert(f(this).name);">2</a>
    </form>    
    <form name="f3">
    <a href="#" onClick="alert(f(this).name);">3</a>
    </form>    
    <form name="f4">
    <a href="#" onClick="alert(f(this).name);">4</a>
    </form>    
      

  3.   

    很简单,你在onClick方法中传入this,
    然后在f函数中添加这个代码:
    function f(obj)
    {
    while (obj.parentElement.tagName != "FORM")
    {
    obj = obj.parentElement;
    }
    obj = obj.parentElement;
    alert(obj.name);
    }Good Luck!
      

  4.   

    下面那样就OK,测试通过!
    <script>
    function f(obj){
        alert(obj.name);
    }
    </script>
    <form name="f1">
    <a href="#" onClick="f(f1)">1</a>
    </form>
    <form name="f2">
    <a href="#" onClick="f(f2)">2</a>
    </form>
    <form name="f3">
    <a href="#" onClick="f(f3)">3</a>
    </form>
      

  5.   

    对于以下四种对象,可以引用form属性.BUTTON, INPUT, SELECT, TEXTAREA 
    例如
    <form name="f1">
    <button onClick="alert(form.name);">Button</button>
    </form> 而A对象,则不可以引用form属性,故楼主的代码会报错
      

  6.   

    to:各位大侠,能在netscape下面运行吗?
      

  7.   

    我给你的那段在IE中测试通过。netscape中能不能用你自己试试
      

  8.   

    不能运行,netscape4.76下面theForms[i].contains和obj.parentElement不能识别,还有没有其他办法呢?
      

  9.   

    form没有名字,例子中不是有吗?
      

  10.   

    to:xzq686(瞬)
    那是我为了确认是访问的哪一个form而写的,实际上是没有名字的.
      

  11.   

    <script>
    function f(obj){
        alert(document.forms[obj].name);
    }
    </script>
    <form name="f1">
    <a href="#" onClick="f(0)">1</a>
    </form>
    <form name="f2">
    <a href="#" onClick="f(1)">2</a>
    </form>
    <form name="f3">
    <a href="#" onClick="f(2)">3</a>
    </form>