element.onclick = fun与element onclick="fun()"的区别

原本标题是:element.onclick = fun与<element οnclick="fun()">的区别,但是提示含有非法字符。

问题背景:

在看this指向的时候看到了这个,有的时候通过给DOM元素绑定监听事件,来触发某些事件处理函数(如点击当前元素时修改该元素样式等),如果绑定的监听函数里面用到了this,上面两种方式可能会有不同的效果。

试验代码:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>区别onclick不同绑定方式</title>
</head>
<body>
<div id="div1" style="width: 80px;height: 80px" >DIV1,点击触发更改颜色</div>
<div id="div2" style="width: 80px;height: 80px" onclick="changeBackground2()">DIV2,点击触发更改颜色</div>
<script>
  //this指向当前元素,本质是调用对象里面的方法
  div1.onclick = changeBackground1;
  function changeBackground1(){
    console.log(this);
    this.style.color = '#cc0000';
  }

  //this指向window对象,本质是调用了嵌套函数,先调用onclick(),再调用changeBackground()
  //嵌套函数中的this要么指向window(非严格模式)要么undefined(严格模式)
  function changeBackground2() {
    console.log(this);
    this.style.color = '#cc0000';  //TypeError:color undefined
  }
</script>
</body>
</html>

试验结果:

可以很明显的看到,第一种方式达到了想要的效果,第二种方式直接报错了。通过控制台打印的内容和报的错能够分析出基本原因,this指向不同,第一个指向了div元素(div1),颜色修改成功,第二个指向了window对象,找不到color属性报错。为什么this的指向会不一样?

原因分析:

1.copying

element.onclick = doSomething;

这个实际上是把doSomthing这个函数复制一份给了element的onclick属性,因此触发点击事件时,this指向的是运行时调用它的对象element。原解释:The function is copied in its entirety to the onclick property (which now becomes a method). So if the event handler is executed this refers to the HTML element and its color is changed.

------------ window --------------------------------------
|                                                        |
|                                                        |
|                                                        |
|   ----------------                                     |
|   | HTML element | <-- this         -----------------  |
|   ----------------      |           | doSomething() |  |
|               |         |           -----------------  |
|          -----------------------          |            |
|          |copy of doSomething()|  <-- copy function    |
|          -----------------------                       |
|                                                        |
----------------------------------------------------------

2.referring

<element onclick="doSomething()">

这种方式只是让onclick属性指向了函数doSomething,并没有把doSomething函数的内部实现也包含进来,因此当触发点击事件时,只是找到onclick属性,然后告诉你去找doSomething吧,我这没具体实现,找到doSomething后,这时候里面的this已经指向window了,其实是在window下调用的doSomething函数。原解释:

you do not copy the function! Instead, you refer to it, and the difference is crucial. The onclick property does not contain the actual function, but merely a function call:

doSomething();

So it says “Go to doSomething() and execute it.” When we arrive at doSomething()the this keyword once again refers to the global window object and the function returns error messages.

------------ window --------------------------------------
|                                          / \           |
|                                           |            |
|                                          this          |
|   ----------------                        |            |
|   | HTML element | <-- this         -----------------  |
|   ----------------      |           | doSomething() |  |
|               |         |           -----------------  |
|          -----------------------         / \           |
|          | go to doSomething() |          |            |
|          | and execute it      | ---- reference to     |
|          -----------------------       function        |
|                                                        |
----------------------------------------------------------

3.两个的区别就是:第一种方式是完全复制;第二种只是指向了函数。

3.1

element.onclick = doSomething;
alert(element.onclick)
//结果如下

function doSomething()
{
	this.style.color = '#cc0000';
}

doSomething是element对象的一个属性(方法),类似下面:

var element = {
            value: "123",
            onclick: function doSomething() {
                console.log(this.value);
            }
        }
element.onclick();//123

3.2 

<element onclick="doSomething()">
alert(element.onclick)
//结果如下

function onclick()
{
	doSomething()
}

doSomething是一个嵌套函数,类似下面:

var element = {
       value: "123",
       onclick: function onclick(){
           doSomething();
       }
    }
function doSomething() {
    console.log(this.value);
}
element.onclick();//undefined

嵌套函数中的this要么指向window(非严格模式),要么undefined(严格模式)

参考链接:https://www.quirksmode.org/js/this.html

https://blog.csdn.net/u013584334/article/details/81192899

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这段代码是一个循环,用于遍历一个 JSON 对象的数组。让我一步一步解释它的作用: 1. `for (let index = 0; index < JSON.parse(obj).length; index++) {`:这行代码定义了一个 for 循环,初始化一个名为 `index` 的变量,初始值为 0。循环的条件是 `index` 小于 `JSON.parse(obj).length`,也就是 JSON 对象的数组长度。 2. `const element = JSON.parse(obj)[index]`:这行代码将当前索引 `index` 对应的元素从 JSON 对象中取出,并赋值给名为 `element` 的常量。 3. `if (element.T === 'T' || element.T === 'Fi') {`:这行代码使用条件语句,检查 `element.T` 的值是否等于 'T' 或 'Fi'。如果满足条件,执行下面的代码块。 4. `content = content + element.V`:这行代码将 `element.V` 的值添加到名为 `content` 的变量中。 5. `else if (element.T === 'F') {`:如果上面的条件不满足,则继续检查 `element.T` 的值是否等于 'F'。 6. `content = content + ' ' + element.V`:如果满足条件,将空格和 `element.V` 的值添加到 `content` 中。 7. `else {`:如果上述两个条件都不满足,则执行下面的代码块。 8. `content = content + ' ' + element.T + '_' + index`:将空格、`element.T` 的值、下划线和当前索引 `index` 的值添加到 `content` 中。 这段代码的目的是根据 JSON 对象的数组元素的类型 `T` 和值 `V`,将对应的内容添加到变量 `content` 中。最后,`content` 变量将包含根据条件添加的内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值