Is var elem a global variable or a local variable, especially when declared within a function object?
PHP中文网
PHP中文网 2017-05-19 10:27:05
0
2
465
function styleHeaderSiblings() {
    if(!document.getElementsByTagName) return false;
    var headers = document.getElementsByTagName("h1");
    var elem;
    for(var i=0; i<headers.length; i++) {
        elem = getNextElement(headers[i].nextSibling);

        elem.style.fontWeight = "bold";
        elem.style.fontSize = "1.2em";
    }
}
PHP中文网
PHP中文网

认证0级讲师

reply all(2)
左手右手慢动作

Just take you for语句里面的var i = 0来说,变量i在函数体内都能访问到,而不仅局限于for inside the loop.

Variables declared with var are within the function scope.

If you want to declare block-level variables, use const或者let.

左手右手慢动作

It is a local variable inside the function and cannot be accessed outside the function.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template