美文网首页
javascript拾遗(1)

javascript拾遗(1)

作者: 非墨即白 | 来源:发表于2018-03-03 12:01 被阅读0次

https://www.quirksmode.org/js/this.html

If you want to usethisfor accessing the HTML element that is handling the event,you must make sure that thethiskeyword is actually written into theonclickproperty.Only in that case does it refer to the HTML element the event handler is registeredto. So if you do

element.onclick = doSomething;

alert(element.onclick)

you get

function doSomething()

{

this.style.color = '#cc0000';

}

As you can see, thethiskeyword is present in theonclickmethod.Therefore it refers to the HTML element.

But if you do

alert(element.onclick)

you get

function onclick()

{

doSomething()

}

This is merely a reference to functiondoSomething(). Thethiskeyword is not present in theonclickmethod so it doesn't refer to the HTML element

相关文章

  • JavaScript 拾遗

    JavaScript 拾遗

  • javascript拾遗(1)

    https://www.quirksmode.org/js/this.html If you want to us...

  • 拾遗神兽目录

    拾遗神兽(番外篇)黑猫 拾遗神兽(1)水晶心的梦 拾遗神兽(2)新宠 拾遗神兽(3)初次交锋猫大爷 拾遗神兽(4)...

  • 你和高手间的差距,就是“简单”

    你和高手间的差距,就是“简单” 我是拾遗君 [营销最前线](javascript:void(0);) 昨天 导读 ...

  • javascript拾遗(2)

    ”JavaScript中的函数运行在它们被定义的作用域里,而不是它们被执行的作用域里.” 全局作用域(Global...

  • JavaScript 拾遗(一)

    JavaScript 保留两位小数 箭头函数ES6 中新增的最激动人心的特性之一。=> 不只是函数关键字 func...

  • JS 拾遗

    重读阮一峰 《JavaScript 标准参考教程》一书。有沧海拾遗的感觉,偶有收获,记录下来,以便之后查阅... ...

  • javascript拾遗之对象

    js是基于简单的object-based paradigm设计的,每一个对象都是一些属性的集合,每一个属性是ke...

  • javascript拾遗之001

    一、JavaScript是什么? 定义:JavaScript是一种小型的、轻量级的、面向对象的、跨平台的客户端脚本...

  • javascript拾遗之002

    一、JavaScript两个常用的客户端输出方法 1.document.write(str):document是一...

网友评论

      本文标题:javascript拾遗(1)

      本文链接:https://www.haomeiwen.com/subject/jwetfftx.html