<body>
<div>
<span id="sp_id">hello,id</span>
<span class="sp_class">hello,class</span>
<span name="sp_name" >hello,name</span>
<b>hello,tag</b>
</div>
</body>
<script type="text/javascript">
function find(el, selector) { //查找子节点,用法类似jquery的find函数,仅支持id,class,attr选择器,仅支持返回匹配的第一个元素
var m = selector.match(/([#\.\[])([\w\W]+)/i);
var type, key,attrName, result;
if (m) {
if (m[1] == ".") {
type = "class"; key = m[2];
} else if (m[1] == "#") {
type = "id"; key = m[2];
} if (m[1] == "[") {
type = "attr";
m = m[2].match(/(\w+)=(\w+)/i);
attrName = m[1];
key = m[2];
}
} else {
type = "tag"; key = selector;
}
function findChild(node) {
var c;
for (var i = 0; i < node.childNodes.length; i++) {
c = node.childNodes[i];
if (type == "class" && c.className == key) {
result = c;
return;
} else if (type == "id" && c.id == key) {
result = c;
return;
} else if (type == "attr" && c.getAttribute && c.getAttribute(attrName) == key) {
result = c;
return;
} else if (type == "tag" && c.tagName && c.tagName.toLowerCase() == key) {
result = c;
return;
}
findChild(c);
}
}
findChild(el);
return result;
}
console.log(find(document.body,"#sp_id").innerHTML);
console.log(find(document.body,".sp_class").innerHTML);
console.log(find(document.body,"[name=sp_name]").innerHTML);
console.log(find(document.body,"b").innerHTML);
</script>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有