js操作element对象(标签)

1、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>

转载于:https://my.oschina.net/u/2524145/blog/637074

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值