总结js中的函数
summarize functions in js
我想总结一些JavaScript功能。例如:
document.getElementByClassName("lamp")[0]; == selector(".lamp")[0];
在这个例子中我没有问题,我的问题是下面的代码:
selector(".lamp")[0].selector(".school")[1].style.color = "red";
当我 运行 这个代码控制台说:
Uncaught TypeError: selector(...)[0].selector is not a function(…)
这是我的选择器函数:
function selector(string){
switch (string[0]) {
case '#':
string = string.replace('#','');
return document.getElementById(string);
break;
case '.':
string = string.replace('.','');
return document.getElementsByClassName(string);
break;
case '<':
string = string.replace('<','');
return document.getElementsByTagName(string);
break;
case '?':
string = string.replace('?','');
return document.getElementsByName(string);
break;
default:
console.log('i cant select it --by selector.js--');
}
}
我该怎么办?
你应该使用原型:
function selector(string){
switch (string[0]) {
case '#':
string = string.replace('#','');
return document.getElementById(string);
break;
case '.':
string = string.replace('.','');
return document.getElementsByClassName(string);
break;
case '<':
string = string.replace('<','');
return document.getElementsByTagName(string);
break;
case '?':
string = string.replace('?','');
return document.getElementsByName(string);
break;
default:
console.log('i cant select it --by selector.js--');
}
}
Object.prototype.selector = function(){
switch (string[0]) {
case '#':
string = string.replace('#','');
return document.getElementById(string);
break;
case '.':
string = string.replace('.','');
return document.getElementsByClassName(string);
break;
case '<':
string = string.replace('<','');
return document.getElementsByTagName(string);
break;
case '?':
string = string.replace('?','');
return document.getElementsByName(string);
break;
default:
console.log('i cant select it --by selector.js--');
}
};
这段代码运行正确无误
我想总结一些JavaScript功能。例如:
document.getElementByClassName("lamp")[0]; == selector(".lamp")[0];
在这个例子中我没有问题,我的问题是下面的代码:
selector(".lamp")[0].selector(".school")[1].style.color = "red";
当我 运行 这个代码控制台说:
Uncaught TypeError: selector(...)[0].selector is not a function(…)
这是我的选择器函数:
function selector(string){
switch (string[0]) {
case '#':
string = string.replace('#','');
return document.getElementById(string);
break;
case '.':
string = string.replace('.','');
return document.getElementsByClassName(string);
break;
case '<':
string = string.replace('<','');
return document.getElementsByTagName(string);
break;
case '?':
string = string.replace('?','');
return document.getElementsByName(string);
break;
default:
console.log('i cant select it --by selector.js--');
}
}
我该怎么办?
你应该使用原型:
function selector(string){
switch (string[0]) {
case '#':
string = string.replace('#','');
return document.getElementById(string);
break;
case '.':
string = string.replace('.','');
return document.getElementsByClassName(string);
break;
case '<':
string = string.replace('<','');
return document.getElementsByTagName(string);
break;
case '?':
string = string.replace('?','');
return document.getElementsByName(string);
break;
default:
console.log('i cant select it --by selector.js--');
}
}
Object.prototype.selector = function(){
switch (string[0]) {
case '#':
string = string.replace('#','');
return document.getElementById(string);
break;
case '.':
string = string.replace('.','');
return document.getElementsByClassName(string);
break;
case '<':
string = string.replace('<','');
return document.getElementsByTagName(string);
break;
case '?':
string = string.replace('?','');
return document.getElementsByName(string);
break;
default:
console.log('i cant select it --by selector.js--');
}
};
这段代码运行正确无误