老師寫的 g方法沒(méi)太看懂 求解function g(selector)
function g(selector){
var method = selector.substr(0,1)=='.'?'getElementByClassName':'getElementById';
return document[method](selector.substr(1));
}
return document[method](selector.substr(1)) 這句話返回的是什么,為什么我用alert(document[method](selector.substr(1))的時(shí)候 返回的是null)
document[method] ?這個(gè)寫法以前沒(méi)見過(guò),是什么意思呢? ?是調(diào)用document 的方法嗎?
2015-12-07
如果method="getElementById",document[method](selector.substr(1))就是document["getElementById"](selector.substr(1)),也就是document.getElementById(selector.substr(1))
2015-12-05
return document[method](selector.substr(1)) 這句話返回的是什么意思呢。?
document[method] 主要是這個(gè)寫法有點(diǎn)接受不了,沒(méi)這么用過(guò)
2015-12-04
function g(selector){
? ? ? ? ? ? ? ? return selector.substring(0, 1) == '.' ? document.getElementsByClassName(selector.substring(1)) : document.getElementById(selector.substring(1));
? ? ? ? ? ? }