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";
}
}
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.