javascript十个最常用的自定义函数
2009-09-08 00:00:00 来源:WEB开发网01.var addLoadEvent = function(fn) {
02. var oldonload = window.onload;
03. if (typeof window.onload != 'function') {
04. window.onload = fn;
05. }else {
06. window.onload = function() {
07. oldonload();
08. fn();
09. }
10. }
11.}
(8) getElementsByClass()
我有收集癖,手头上拥有许多版本,最后集思广益自己实现了一个。下面是我的实现:
01.var getElementsByClassName = function (searchClass, node,tag) {
02. if(document.getElementsByClassName){
03. return document.getElementsByClassName(searchClass)
04. }else{
05. node = node || document;
06. tag = tag || "*";
07. var classes = searchClass.split(" "),
08. elements = (tag === "*" && node.all)? node.all : node.getElementsByTagName(tag),
09. patterns = [],
10. returnElements = [],
11. current,
12. match;
13. var i = classes.length;
14. while(--i >= 0){
15. patterns.push(new RegExp("(^|\s)" + classes[i] + "(\s|$)"));
16. }
17. var j = elements.length;
18. while(--j >= 0){
19. current = elements[j];
20. match = false;
21. for(var k=0, kl=patterns.length; k<kl; k++){
22. match = patterns[k].test(current.className);
23. if (!match) break;
24. }
25. if (match) returnElements.push(current);
26. }
27. return returnElements;
28. }
29.}
Tags:javascript 十个 常用
编辑录入:爽爽 [复制链接] [打 印]更多精彩
赞助商链接