您好,欢迎访问一九零五行业门户网

javascript如何删除子节点属性

在javascript中,可以使用removeattribute方法删除子节点属性,语法格式为“removeattribute(元素的属性名)”。removeattribute方法删除指定的属性。
本教程操作环境:windows7系统、javascript1.8.5版、dell g3电脑。
在 javascript 中,使用元素的 removeattribute() 方法可以删除指定的属性。用法如下:
removeattribute(name)
参数 name 表示元素的属性名。
示例1下面示例演示了如何动态设置表格的边框。
<script> window.onload = function () { //绑定页面加载完毕时的事件处理函数 var table = document.getelementbytagname("table")[0]; //获取表格外框的引用 var del = document.getelementbyid("del"); var reset = document.getelementbyid("reset"); del.onclick = function () { table.removeattribute("border"); } reset.onclick = function () { table.setattribute("border", "2"); }</script><table width="100%" border="2"> <tr> <td>数据表格</td> <tr></table><button id="del">删除</button><button id="reset">恢复</button>
在上面示例中设计了两个按钮,并分别绑定了不同的事件处理函数。单击“删除”按钮即可调用表格的 removeattribute() 方法清除表格边框,单击“恢复”按钮即可调用表格的 setattribute() 方法重新设置表哥便可的粗细。
示例2下面示例演示了如何自定义删除类函数,并调用该函数删除指定类名。
<script> function hasclass (element, classname) { //类名检测函数 var reg = new regexp ('(\\s|^)' + classname + '(\\s|$)'); return reg.test (element, classname); //使用正则检测是否有相同的样式 } function deleteclass (element, classname) { if (hasclass (element, classname)) { element.classname.replace (reg, ' '); //捕获要删除样式,然后替换为空白字符串 } }</script><p id="red" class="red blue bold">盒子</p><script> var red = document.getelementbyid ("red"); deleteclass (red, 'blue');</script>
上面代码使用正则表达式检测 classname 属性值字符串中是否包含指定的类名,如果存在,则使用空字符串替换掉匹配到的子字符串,从而实现删除类名的目的。
removeattribute与removeattributenode方法异同removeattribute
移除节点指定名称的属性。示例如下
document.getelementbyid('risktypepie').removeattribute("style");
removeattributenode
注:此方法不兼容ie。
使用方法:
获取要删除属性的元素
获取该元素要删除的属性
<元素>.removeattributenode<属性>
var node=document.getelementbyid('chartwrap');var attr=n.getattributenode('style');node.removeattributenode(attr);
异同分析
相同点
两个方法都是用来移除节点属性
两种方法调用者都只能是标签节点
不同点
removeattribute方法接收的是要删除属性的名字
removeattributenode方法接收的是要删除的属性节点它本身
javascript动态添加、修改、删除对象的属性与方法详解【推荐学习:javascript高级教程】
以上就是javascript如何删除子节点属性的详细内容。
其它类似信息

推荐信息