getElementsByClassName


//code ref http://www.cnblogs.com/rubylouvre/archive/2009/07/24/1529640.html
//支持 多class搜索 ie:getElementsByClassName("classA classB")
var getElementsByClassName = function (searchClass, node, tag) {
var node = node || document;
var tag = tag || "*";
if (document.getElementsByClassName) {//尽量用原生方法
var nodes = node.getElementsByClassName(searchClass),result = [];
if (tag === "*") {//直接return
return nodes;
} else {
for (var node in nodes) {
if (node.tagName === tag.toUpperCase()) {
result.push(node)
}
}
return result
}
} else {
var classes = searchClass.split(" "),
elements = (tag === "*" && node.all) ? node.all : node.getElementsByTagName(tag),
patterns = [],
current,
match;
var i = classes.length;
while (i--) {
patterns.push(new RegExp("(^|\\s)" + classes[i] + "(\\s|$)"));
}
var j = elements.length;
while (j--) {
current = elements[j];
match = false;
for (var pattern in patterns) {
match = pattern.test(current.className);
if (!match) break;
}
if (match) result.push(current);
}
return result;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值