walkdom
function walkthedom(node, func) { func(node); node = node.firstchild; while (node) { walkthedom(node, func); node = node.nextsibling; } }
getelementsbyclassname
function getelementsbyclassname(classname) { var results = []; walkthedom(document.body, function (node) { var a, c = node.classname, i; if (c) { a = c.split(' '); for (i = 0; i < a.length; i += 1) { if (a[i] === classname) { results.push(node); break; } } } }); return results; }
第六章
6.5 判断是不是数组
var is array = function (value) { return value && typeof value.splice === 'function' && typeof value.length=== 'number' && typeof value === 'object' && ! (value.propertyisenumerable ('length')); };
写一个reduce
array. method ('reduce', function(f,value) { var i; for (i=0;i<this.length; i +=1) { value = f(this [i], value); } return value; })
第七章 正则表达式
7.1 匹配url的正则
var parse_url =/^ (?: ([a-za-z]+) :)? (\/{0,3}) ( [0-9.\-a-za-z]+)(?:: (d+))? (?:\/([^?#]*))?(?:\? ([^#]*))? (?:#(.*))?$/; var url ="http://www.ora.com: 80/goodparts?q#fragment";
匹配数字的reg
var parse_number =/^-?\d+(?:\.\d*)? (?:e [+\-]?\dt)?s/i; var test= function (num) { document.writeln(parse number.test (num) ); }; true test ("1"); false test ('number'); true test('98.6"); false test ('132.21.86.100'); true test ('123.45e-67'); false test ('123.45d-67');
如对本文有疑问, 点击进行留言回复!!
网友评论