<ul class="list" id="list">
<li class="in"></li>
<li class="in"></li>
</ul>
<script>
var oList = document.getElementById('list');
var children = oList.childNodes;
var num = 0;
for(var i = 0; i < children.length; i++){
if(children[i].nodeType == 1){
num++;
}
}
console.log(num);//2 有2个元素子节点
</script>
<div class="wrapper" id='test' for="nice" style="width:200px;height:100px;background:#f0f">123</div>
<script type="text/javascript">
var oTest = document.getElementById('test');
//IE6/IE7不支持hasAttribute方法
console.log(oTest.hasAttribute('class'));//true
console.log(oTest.hasAttribute('className'));//false
console.log(oTest.hasAttribute('id'));//true
console.log(oTest.hasAttribute('style'));//true
console.log(oTest.hasAttribute('for'));//true
console.log(oTest.hasAttribute('htmlFor'));//false
</script>
<script type="text/javascript">
var oTest = document.getElementById('test');
oTest.setAttribute('class','aaa'); //setAttribute直接用class就可以了
oTest.setAttribute('className','bbb');
console.log(oTest.class);//undefined IE8及以下会报错缺少标识符
console.log(oTest.getAttribute('class'));//aaa getAttribute直接用class就可以了
console.log(oTest.className);//aaa
console.log(oTest.getAttribute('className'));//bbb
oTest.setAttribute('style','border:1px solid red;height: 100px;'); //setAttribute直接用 style 就可以了
console.log(oTest.style);//所有的style设置,包括你没有设置的,太多了,肯定不是你想要的
console.log(oTest.getAttribute('style'));
//border:1px solid red;height: 100px; getAttribute直接用 style 就可以了
oTest.setAttribute('for','eee'); //setAttribute直接用for就可以了
oTest.setAttribute('htmlFor','fff')
console.log(oTest.for);//undefined IE8及以下会报错缺少标识符
console.log(oTest.htmlFor);//undefined
console.log(oTest.getAttribute('for'));//eee getAttribute直接用for就可以了
console.log(oTest.getAttribute('htmlFor'));//fff
console.log(oTest);
//<div class="aaa" id="test" for="eee" style="ddd" classname="bbb" htmlfor="fff">123</div>
</script>
<div class="wrapper" id='test' for="nice" style="background:#f0f;height: 100px;">123</div>
<script type="text/javascript">
var oTest = document.getElementById('test');
oTest.removeAttribute('class'); //removeAttribute直接用class就可以了
oTest.removeAttribute('for');
oTest.removeAttribute('style');
console.log(oTest);// <div id="test">123</div>
</script>
<div class="wrapper" id='test' for="nice" style="background:#f0f;height: 100px;">123</div>
<script type="text/javascript">
var oTest = document.getElementById('test');
console.log(oTest.attributes);// NamedNodeMap {0: class, 1: id, 2: for, 3: style, length: 4}
console.log(oTest.attributes.item(1).specified);//true
console.log(oTest.attributes.getNamedItem('id'));//id='test'
console.log(typeof oTest.attributes.getNamedItem('id'));//object
console.log(oTest.attributes.removeNamedItem('for'));//id='test'
console.log(oTest.attributes);// NamedNodeMap {0: class, 1: id, 2: style, length: 3}
var abc = document.createAttribute("abc");
abc.nodeValue = "1234567";
oTest.attributes.setNamedItem(abc);
//obj.attributes.setNamedItem(attr) 要先创建属性,在以nodeValue的形式赋属性值,在传入setNamedItem
console.log(oTest.attributes);// NamedNodeMap {0: class, 1: id, 2: style, 3: abc, length: 4}
console.log(oTest.attributes.item(1));//id='test'
console.log(oTest.attributes[1]);//id='test'
</script>
<div class="wrapper" id='test' for="nice" style="background:#f0f;height: 100px;">123</div>
<script type="text/javascript">
function outputAttributes(obj){
var arr = [],
attrName,
attrValue,
i;
for(i = 0; i < obj.attributes.length; i++){
attrName = obj.attributes[i].nodeName;
attrValue = obj.attributes[i].nodeValue;
arr.push(attrName + '=\"' + attrValue + '\"');
}
return arr.join(" ");
}
var oTest = document.getElementById('test');
console.log(oTest.attributes);//NamedNodeMap {0: class, 1: id, 2: for, 3: style, length: 4}
console.log(typeof oTest.attributes);//object
console.log(outputAttributes(oTest));
//class="wrapper" id="test" for="nice" style="background:#f0f;height: 100px;"
console.log(typeof outputAttributes(oTest));//string
</script>
<div id="test">
<div>hello world!</div>
</div>
<script type="text/javascript">
var oTest = document.getElementById('test');
//第一个和最后一个都是空白文本节点
console.log(oTest.firstChild);//" "
console.log(oTest.lastChild);//" "
console.log(oTest.childNodes);//[text, div, text]
//标准浏览器输出[text, div, text],text表示空白文本节点
//IE8及以下浏览器输出[div],并不包含空白文本节点
console.log(oTest.childNodes.length); //3
//标准浏览器返回3
//IE8及以下浏览器返回1,并不包含空白文本节点
//清理空白文本节点
function cleanWhitespace(oEelement){
for(var i = 0; i < oEelement.childNodes.length; i++){
var node = oEelement.childNodes[i];
if(node.nodeType == 3 && !/\S/.test(node.nodeValue)){
node.parentNode.removeChild(node);
}
}
}
cleanWhitespace(oTest);
console.log(oTest.childNodes);//[div]
console.log(oTest.childNodes.length); //1
</script>
<div id="testData">hello world!</div>
<div id="testWholeText">hello world!</div>
<script type="text/javascript">
var oTestData = document.getElementById('testData');
//第一个和最后一个都是空白文本节点
console.log(oTestData.firstChild);//"hello world!"
console.log(typeof oTestData.firstChild);//object
console.log(oTestData.childNodes.length); //1
console.log(oTestData.firstChild.nodeValue);//"hello world!"
console.log(typeof oTestData.firstChild.nodeValue);//string
console.log(oTestData.firstChild.data);//"hello world!"
//文本节点的data属性与nodeValue属性相同,都是 string 类型
console.log(oTestData.firstChild.data === oTestData.firstChild.nodeValue);//true
var oTestWholeText = document.getElementById('testWholeText');
console.log(oTestWholeText.childNodes); //[text]
console.log(oTestWholeText.childNodes.length); //1
console.log(oTestWholeText.firstChild.wholeText);//hello world!
console.log(oTestWholeText.firstChild.data);//hello world!
oTestWholeText.firstChild.splitText('or');
console.log(oTestWholeText.childNodes); //[text, text]
console.log(oTestWholeText.childNodes.length); //2
console.log(oTestWholeText.firstChild);//#text
console.log(oTestWholeText.firstChild.wholeText);//hello world!
//wholeText属性将当前Text节点与毗邻的Text节点,作为一个整体返回。
console.log(oTestData.firstChild.length);//12
console.log(oTestData.firstChild.nodeValue.length);//12
console.log(oTestData.firstChild.data.length);//12
</script>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有