当通过javascript定义时,onclick的行为有所不同 [英] onclick behaves differently when defined via javascript

查看:69
本文介绍了当通过javascript定义时,onclick的行为有所不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://www.frostjedi.com /terra/scrip...his-alert.html
http://www.frostjedi.com/terra/scrip...is-alert2.html


为什么,当你点击在黑匣子里,做警示框说不同的东西是什么?他们不应该说同样的话吗?

http://www.frostjedi.com/terra/scrip...his-alert.html
http://www.frostjedi.com/terra/scrip...is-alert2.html

Why, when you click in the black box, do the alert boxes say different
things? Shouldn''t they say the same thing?

推荐答案

2008年9月25日星期四15:45:50 -0700,yawnmoth写道:
On Thu, 25 Sep 2008 15:45:50 -0700, yawnmoth wrote:
http: //www.frostjedi.com/terra/scrip...his-alert.html
http://www.frostjedi.com/terra/scrip...is-alert2.html


为什么,当你在黑匣子里点击时,警告框会说不同的

东西?他们不应该说同样的话吗?
http://www.frostjedi.com/terra/scrip...his-alert.html
http://www.frostjedi.com/terra/scrip...is-alert2.html

Why, when you click in the black box, do the alert boxes say different
things? Shouldn''t they say the same thing?



你的问题在这里:

< div onclick =" test()"< / div>


它不同于:

< div onclick = test< / div>


第一个表单运行"测试()"在全球范围内运作。

全局对象没有名为tagName的属性。

第二种形式(以及你的第二个例子)使用函数

参考,并在div的上下文中运行。


了解脚本运行的环境对于理解什么是这个是必不可少的。是指。

Your problem is here:
<div onclick="test()"</div>

It is NOT the same as:
<div onclick=test</div>

The first form runs the "test()" function within the global context. The
Global Object does not have a property called tagName.
The second form (as well as your second example) use a function
reference, and runs within the context of the div.

Understanding the environment the script runs in is essential to
understanding what "this" refers to.


当为元素提供内在事件属性时(假设

被识别,等等),例如: -


< div onclick =" test()"< / div>


- 浏览器使用字符串值(在本例中为text())作为

正文文本的一个函数,它创建并分配给

对应的表示属性

DOM中的元素。所以这里的浏览器相当于: -


divRef.onclick = function(event){

test();

};


(有或没有 - event - 形式参数,取决于

浏览器)


浏览器执行此操作与执行

相当于: -


divRef.onclick = test;


- 是当浏览器调用函数时它调用函数

- divRef.onclick(); - (有或没有事件对象作为

参数,具体取决于浏览器)所以 - this - 值为

执行分配给 - divRef的函数。 onclick - 是对DIV元素的一个

引用,但分配的函数是不同的。

一个是浏览器创建的函数(该函数将

然后调用 - test - )而另一个是测试本身。如果

被调用的函数是由浏览器创建的函数那么当它被调用时 - 测试 - 它会以这样的方式实现 - 这个 - 价值

在该调用中是对全局对象的引用。
When an intrinsic event attribute is provided for an element (assuming
it is recognised, etc.), such as:-

<div onclick="test()"</div>

- the browser uses the string value (the "text()" in this case) as the
body text of a function that it creates and assigned to the
corresponding property of the representation of the element in the
DOM. So what the browser does here is the equivalent of:-

divRef.onclick = function(event){
test();
};

(with or without the - event - formal parameter, depending on the
browser)

The difference between the browser doing this and your doing the
equivalent of:-

divRef.onclick = test;

- is that when the browser calls the function it calls the function as
- divRef.onclick(); - (with or without an event object as the
argument, depending on the browser) so the - this - value for the
execution of the function assigned to - divRef.onclick - is a
reference to the DIV element, but the function assigned is different.
One is the function that the browser created (the function that will
then call - test -) and the other is test itself. In the event that
the function called is the one created by the browser then when it
calls - test - it does so in a way that will make the - this - value
inside that call be a reference to the global object.


9月26日,12:43 * pm,Henry< rcornf。 .. @ raindrop.co.ukwrote:
On Sep 26, 12:43*pm, Henry <rcornf...@raindrop.co.ukwrote:

为元素提供内在事件属性时(假设

被识别,等等。),例如: -


< div onclick =" test()"< / div>


- 浏览器使用字符串值(在本例中为text())作为它创建并分配给

对应属性的函数的

正文文本

DOM中元素的表示形式。所以这里的浏览器相当于: -


divRef.onclick = function(event){

* * test();


};
When an intrinsic event attribute is provided for an element (assuming
it is recognised, etc.), such as:-

<div onclick="test()"</div>

- the browser uses the string value (the "text()" in this case) as the
body text of a function that it creates and assigned to the
corresponding property of the representation of the element in the
DOM. So what the browser does here is the equivalent of:-

divRef.onclick = function(event){
* * test();

};



纠正我,如果我弄错了,但我想我曾经在某个地方看过它

执行文本的eval():喜欢(在这种情况下)eval(''test()'')...?


-

Jorge。

Correct me if I''m mistaken, but I think I once read somewhere that it
does an eval() of the text: like (in this case) eval(''test()'')... ?

--
Jorge.


这篇关于当通过javascript定义时,onclick的行为有所不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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