是关于作用域的问题,代码如下:
<!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=UTF-8">
<title>Insert title here</title>
<script type='text/javascript' src='js/liukai.js'></script>
<script type='text/javascript'>
window.onload = function(){
//var e = document.createEventObject();
var element = document.getElementById('id2');
//element.fireEvent('ok',e);
element.attachEvent('onclick',function(){alert('ok!');});
element.fireEvent('onclick');
}

window.onerror = function(msg,url,line){
if(onerror.num ++ < onerror.max){
alert('发生错误:' + msg + '\n地址:' + url + '\n行数:' + line);
return true;
}
}
onerror.num = 0;
onerror.max = 3;</script>
</head>
<body>
<form>
<tr>
<td><input id='id2' type='text' value='asdasd' onclick="alert(forms[forms.length - 1].childNodes.item(2).nodeName);"/></td>
<td></td><a href="#" id="aa">asdasdasdasda</a></td>
</tr>
</form>
</body>
</html>
这样写没问题,但是为什么这样就错了?
<td><input id='id2' type='text' value='asdasd' onclick="alert(this.forms[forms.length - 1].childNodes.item(2).nodeName);"/></td>
而这样又是对的:
<td><input id='id2' type='text' value='asdasd' onclick="alert(this.form.childNodes.item(2).nodeName);"/></td>
我就搞不懂了,请教一下大家,第二个写法为什么不对。
这个例子是IE下面的,谢谢大家

解决方案 »

  1.   

    this.forms为空,表单元素不存在forms属性
    所有表单元素有form属性,返回其所在的表单对象
      

  2.   

    我知道怎么写是对的,我就是想搞清楚那个this是什么,是body还是document,为什么this.form取得到,this.forms[0]取不到
      

  3.   

    LZ你说呢:
    <!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=UTF-8">
    <title>Insert title here</title><body>
    <form>
        <tr>
            <td><input id='id2' type='text' value='asdasd' onclick="alert(this.forms.length);"/></td>
            <td></td><a href="#" id="aa">asdasdasdasda</a></td>
        </tr>
    </form>
    </body>
    </html>
    自已去试吧!你再去掉forms的s再试
      

  4.   

    你哪个form里面有form么?想什么呢?
      

  5.   

    如果说this是指form,那么
    <td><input id='id2' type='text' value='asdasd' onclick="open()"/></td>
    却没有报错,说明this应该有个open方法,应该不是form吧
      

  6.   


    <td><input id='id2' type='text' value='asdasd' onclick="alert(this.tagName);"/></td>
      

  7.   

    挺郁闷的,我写forms是因为document有个forms集合,我只是想看看this是不是document而已,汗
      

  8.   


    <td><input id='id2' type='text' value='asdasd' onclick="alert(this.tagName);"/></td>
      

  9.   

    我想this在这里肯定是来回变得,不然,难道这里还有个open方法吗,或者form属性,应该不是指自己那么简单吧,好昏呀
      

  10.   


    <!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=UTF-8">
    <title>Insert title here</title><body>
    <form name="2">
         
        <tr>
            <td><input id='id2' type='text' value='asdasd' onclick="alert(this.form.name);"/></td>
            <td></td><a href="#" id="aa">asdasdasdasda</a></td>
        </tr>
         
    </form>
    </body>
    </html>回家吃饭了!自已想去吧
      

  11.   

    我想得很清楚了,你那样写this当然是指那个form,你直接写this.tagName,就应该是FORM才对,可是结果是INPUT,我写open(),也不会报错,也不是window的方法,因为没有新窗口打开,也肯定不是form或者input的方法。难道是我移开的意图没表达清楚,我是想知道为什么this.forms是错的。
      

  12.   

    this.form vs document.forms. 
      

  13.   

    <input onclick='alert([this,this.form,open])'  >this指定 input标签
    this.from 是指 input标签本身引用的 外部from 所以是成功的open()存在  是因为  open 的调用没有指定open所在的区域  默认就是  调用 window.open()
      

  14.   

    归根结底还是this的问题啊!!!
    楼主去看一看JS的this再说吧,this随着上下文是会变化的哦。