标签:href vs. onClick [英] A tag: href vs. onClick

查看:74
本文介绍了标签:href vs. onClick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅


< ul>

< li>< a name =" link1" onClick =" alert(this.name); return false;"

href ="#"> Link1< / a>< / li>

< li>< a name =" link2" href =" javascript :alert(this);"> Link2< / a>< / li>

< li> Item 3< / li>

< / ul>


点击第一个列表项会给我link1因为这个将
引用到A节点,以便this.name返回该节点的名称属性值




但是在第二个链接,this似乎是指窗口节点!


这是为什么?为什么这个的含义是什么?

在href = javascript 中使用时变化如此剧烈:与onClick =


谢谢

See

<ul>
<li><a name="link1" onClick="alert(this.name);return false;"
href="#">Link1</a></li>
<li><a name="link2" href="javascript:alert(this);">Link2</a></li>
<li>Item 3</li>
</ul>

Clicking on the first list item gives me "link1" because "this" refers
to the A node so this.name returns the value of the name attribute of
that node.

But in the second link, "this" seems to refer to the window node!

Why is this? Why does the meaning of "this" change so drastically when
used in a href=javascript: vs. a onClick=

Thanks

推荐答案

us****@vikas.mailshell.com 在2005年10月22日11:00 PM说:
us****@vikas.mailshell.com said the following on 10/22/2005 11:00 PM:
参见

< ul>
< li>< a名称= QUOT; LINK1" onClick =" alert(this.name); return false;"
href ="#"> Link1< / a>< / li>
< li>< a名称= QUOT; LINK2" href =" javascript :alert(this);"> Link2< / a>< / li>
< li> Item 3< / li>
< / ul>

点击第一个列表项会给我link1因为这个指向A节点,因此this.name返回该节点的name属性的值。

但在第二个链接中,this指的是该节点。似乎是指窗口节点!


Yeppers。因为javascript :伪协议是在

全局上下文中执行的。

为什么会这样?为什么这个的含义是什么?
在href = javascript 中使用时变化如此剧烈:与onClick =
See

<ul>
<li><a name="link1" onClick="alert(this.name);return false;"
href="#">Link1</a></li>
<li><a name="link2" href="javascript:alert(this);">Link2</a></li>
<li>Item 3</li>
</ul>

Clicking on the first list item gives me "link1" because "this" refers
to the A node so this.name returns the value of the name attribute of
that node.

But in the second link, "this" seems to refer to the window node!
Yeppers. Because the javascript: pseudo-protocol is executed in the
global context.
Why is this? Why does the meaning of "this" change so drastically when
used in a href=javascript: vs. a onClick=




这不会改变,它的执行环境有什么变化。


不要使用javascript :,使用onclick你就没有问题。


-

兰迪

comp.lang.javascript常见问题 - http://jibbering.com/faq &新闻组周刊



this doesn''t change, what changes is the context in which it is executed.

Don''t use javascript:, use the onclick and you don''t have that problem.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly


us **** @ vikas.mailshell.com 说:

请参阅

< ul>
< li>< a name =" link1" onClick =" alert(this.name); return false;"
href ="#"> Link1< / a>< / li>
< li>< a名称= QUOT; LINK2" href =" javascript :alert(this);"> Link2< / a>< / li>
< li> Item 3< / li>
< / ul>

点击第一个列表项会给我link1因为这个指向A节点,因此this.name返回该节点的name属性的值。

但在第二个链接中,this指的是该节点。似乎是指窗口节点!

为什么会这样?为什么这个的含义是什么?在href = javascript 中使用
时会发生如此剧烈的变化:与onClick

See

<ul>
<li><a name="link1" onClick="alert(this.name);return false;"
href="#">Link1</a></li>
<li><a name="link2" href="javascript:alert(this);">Link2</a></li>
<li>Item 3</li>
</ul>

Clicking on the first list item gives me "link1" because "this" refers
to the A node so this.name returns the value of the name attribute of
that node.

But in the second link, "this" seems to refer to the window node!

Why is this? Why does the meaning of "this" change so drastically when
used in a href=javascript: vs. a onClick




onclick处理程序是一种方法链接。 javascript :"

伪协议是一种将新内容加载到页面中的机制。

例如:


< a href =" javascript :( new Date())。toLocaleString()">几点了?< / a>


使用它执行任意函数是对其预期的

函数的反转。由于它正在将新内容加载到页面中,因此页面

(即窗口)是其执行上下文。



The onclick handler is a method of the link. The "javascript:"
pseudo-protocol is a mechanism for loading new content into the page.
For example:

<a href="javascript:(new Date()).toLocaleString()">What time is it?</a>

Using it to execute arbitrary functions is a perversion of its intended
function. Since it is loading new content into the page, the page
(ie, the window) is its execution context.


> < a name =" link2" href =" javascript :alert(this);"> Link2< / a>
> <a name="link2" href="javascript:alert(this);">Link2</a>
< a href =" javascript :( new Date())。toLocaleString()">几点了?< / a>
<a href="javascript:(new Date()).toLocaleString()">What time is it?</a>




我只是再次指出你*从来没有*应该使用

javascript :someFunction()syntacs。当你用由
someFunction生成的新HTML页面替换当前页面时,唯一的地方和时间是




从javascript 调用JavaScript:在同一页面上停留时,你需要使用void()包装器:

javascript < b> :void(无论你想要什么)

否则它会严重误用javascript

pseudo-协议。



I''d just like to point again that you *never ever* should use
javascript:someFunction() syntacs. The only place and time for it is
when you''re replacing the current page by a new HTML page generated by
someFunction.

To call JavaScript from javascript: while staying on the same page you
have to use void() wrapper:
javascript:void(whateverYouWant)
Otherwise it''s going to be a gross misuse of javascript:
pseudo-protocol.


这篇关于标签:href vs. onClick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆