JavaScript基础求助

tale12 2010-02-08 11:45:30
是关于作用域的问题,代码如下:
<!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下面的,谢谢大家
...全文
136 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
acRush7 2010-02-09
  • 打赏
  • 举报
回复
this.form vs document.forms.
tale12 2010-02-09
  • 打赏
  • 举报
回复
不讨论了,小弟在某本书上看到答案了。
大家说的都不太对,咳咳
浴火_凤凰 2010-02-09
  • 打赏
  • 举报
回复
归根结底还是this的问题啊!!!
楼主去看一看JS的this再说吧,this随着上下文是会变化的哦。
KK3K2005 2010-02-09
  • 打赏
  • 举报
回复
<input onclick='alert([this,this.form,open])' >

this指定 input标签


this.from 是指 input标签本身引用的 外部from 所以是成功的

open()存在 是因为 open 的调用没有指定open所在的区域 默认就是 调用 window.open()
tale12 2010-02-08
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 xiaofanku 的回复:]
HTML code<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><metahttp-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title><body><formname="2"><tr><td><inputid='id2'type='text'value='asdasd'onclick="alert(this.form.name);"/></td><td></td><ahref="#" id="aa">asdasdasdasda</a></td></tr></form></body></html>
回家吃饭了!自已想去吧
[/Quote]
我想得很清楚了,你那样写this当然是指那个form,你直接写this.tagName,就应该是FORM才对,可是结果是INPUT,我写open(),也不会报错,也不是window的方法,因为没有新窗口打开,也肯定不是form或者input的方法。难道是我移开的意图没表达清楚,我是想知道为什么this.forms是错的。。。。
街头小贩 2010-02-08
  • 打赏
  • 举报
回复

<!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>

回家吃饭了!自已想去吧
tale12 2010-02-08
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 xiaofanku 的回复:]
HTML code<td><inputid='id2'type='text'value='asdasd'onclick="alert(this.tagName);"/></td>
[/Quote]
我想this在这里肯定是来回变得,不然,难道这里还有个open方法吗,或者form属性,应该不是指自己那么简单吧,好昏呀
街头小贩 2010-02-08
  • 打赏
  • 举报
回复

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

tale12 2010-02-08
  • 打赏
  • 举报
回复
挺郁闷的,我写forms是因为document有个forms集合,我只是想看看this是不是document而已,汗
街头小贩 2010-02-08
  • 打赏
  • 举报
回复

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

tale12 2010-02-08
  • 打赏
  • 举报
回复
如果说this是指form,那么
<td><input id='id2' type='text' value='asdasd' onclick="open()"/></td>

却没有报错,说明this应该有个open方法,应该不是form吧
街头小贩 2010-02-08
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 tale12 的回复:]
我知道怎么写是对的,我就是想搞清楚那个this是什么,是body还是document,为什么this.form取得到,this.forms[0]取不到
[/Quote]
你哪个form里面有form么?想什么呢?
街头小贩 2010-02-08
  • 打赏
  • 举报
回复
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再试
tale12 2010-02-08
  • 打赏
  • 举报
回复
我知道怎么写是对的,我就是想搞清楚那个this是什么,是body还是document,为什么this.form取得到,this.forms[0]取不到
chrome_ 2010-02-08
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 xmliy 的回复:]
应该是document.forms吧
[/Quote]
this.forms为空,表单元素不存在forms属性
所有表单元素有form属性,返回其所在的表单对象
xmliy 2010-02-08
  • 打赏
  • 举报
回复
应该是document.forms吧

87,955

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧