Home  >  Article  >  Web Front-end  >  在html元素中定义事件时,注意作用域链的不同_html/css_WEB-ITnose

在html元素中定义事件时,注意作用域链的不同_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 11:51:361069browse

在html元素中定义事件时

1、如果不是调用的函数,那么这里的作用域链是:

调用对象---->事件源---->父元素----->>document----->window

例如:

<script></script>

    var node = document.getElementById('fp')

    node.age = 12;

    age = 13

结果:12

2、如果是调用的函数,由于JS的词法作用域(函数在定义它的作用域中执行,而不是在调用它的作用域中执行),那么这里的作用域链是:调用对象----->>window

例如:

<script></script>

    var node = document.getElementById('fp')

    node.age = 12;

    age = 13

    function msg(){

        alert(age);

    }

结果:13

3、如果是在JS中定义事件,那作用域链也是调用对象----->>window

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn