js操做element对象(标签)

一、js操做element对象

<input type="text" name="uNamesss" id="id1" value="abc"/>
<ul id="ul2">
 <li>uk</li>
 <li>usa</li>
</ul>
<script type="text/javascript">
 //获取到ul下面的子元素
 var ul2 = document.getElementById("ul2");
 var arr = ul2.childNodes;
 alert("length1:  "+arr.length);// 2
 var arr1 = ul2.getElementsByTagName("li");
 alert("length2: "+arr1.length); //2
 //获取到input
 var inputs = document.getElementById("id1");
 alert(inputs.value); //abc
 alert(inputs.getAttribute("name"));//uNamesss
 //setAttribute
 alert(inputs.getAttribute("class")); //null
 inputs.setAttribute("class","haha");
 alert(inputs.getAttribute("class"));//haha
 //removeAttribute
 alert(inputs.getAttribute("name"));//uNamesss
 inputs.removeAttribute("name");
 alert(inputs.getAttribute("name"));//显示空串
 //注意经过removeAttribute,不能删除value
 alert(inputs.getAttribute("value"));//abc
 inputs.removeAttribute("value");
 alert(inputs.getAttribute("value"));//abc
</script>
相关文章
相关标签/搜索