day11
①高级DOM操作
节点类型
1.元素节点--------nodeType = 1
2.文本节点--------nodeType = 3
3.属性节点--------nodeType = 2
封装一个获取指定类型节点的方法
function( ele, nodeType ) {
var type = {
"Text" : 1,
"Element" : 2,
"Attribute" : 3,
"text" : 1,
"element" : 2,
"attribute" : 3,
"1" : 1,
"2" : 2,
"3" : 3
}
nodeType = nodeType || 1;
if ( !( nodeType.toString() in type ) ) {
throw "Arguments Error!";
}
var res = [];
var all = ele.childNodes;
for ( var i = 0; i < all.length; i ++) {
if ( all[i].nodeType == type[nodeType.toString()] ) {
res.push( all[i] );
}
}
return res;
}
②查错处理
try {
//尝试运行代码
} catch(e) {
//当拦截到异常消息时进行处理
//当一个异常消息被catch时,错误会被掩盖,不影响(若与try中代码没有因果关系)后面的代码运行
}
throw new Error(); //手动的创建一个异常消息,并向上级抛出
③正则表达式
用特定符号所描述的一种规则
由Perl最先提出并使用
可以大量节省验证带来的工作量
//第一种
var reg = new RegExp("google","gi");
//第二种
/google/gi // /google/gi.test(str)检测字符串是否符合
str.match()//返回组成的数组
str.search()//返回第一个
str.replace()//替换符合正则的
//符号匹配规则
.任意字符
*表示任意次匹配(包括0)
?有或没有
+表示至少1次
{m,n}至少m次最多n次
{m,}至少m次
字符匹配
[a-zA-Z0-9] 列举所有符合的情况,当情况过多时,用中括号表示范围
④位置偏移获取
offsetWidth / clientWidth(计算元素可视宽度,边框计算在内)
offsetHeight / clientHeight(计算元素可视高度,边框计算在内)
**offsetLeft** 相对于最近的有定位的祖先元素
**offsetTop** 相对于最近的有定位的祖先元素
offsetParent 获取参照物