『壹』 js用正则表达式删除指定属性
varsource='<divstyle="a:a;b:b;c:c;"width="10%"height="dd"></div>';
//删除width属性及值。
console.log(source.replace(/width=".*?"/,''));
//删除style属性中的键值对b和c
console.log(source.replace(/(style=")(.*?)(")/,function(m,g1,g2,g3){
returng1+g2.replace(/s?(.*?):(.*?);/g,function(m,g1,g2){
//删除b属性和c属性要改成其他属性可版以在这里控制权
if(/^(b|c)$/.test(g1))
return'';
returnm;
})+g3;
}));
『贰』 如何用js删除 dom元素的嵌入样式(style)的一个属性
将style中你想删掉的属性设为null应该是可以解决的,Chrome通过
例如:
elem.style["height"] = null;//彻底删除elem的style属性的height值
『叁』 js清除object
删除对象,直接使用delete就可以了。
比如:delete document.getElementById('div');
删除一个属性的过程也很简单,就是将其置为undefined:
user.name=undefined;
user.alert=undefined;
这样就删除了name属性和alert方法。在之后的代码中,这些属性变的不可用。
在添加、修改或者删除属性时,和引用属性相同,也可以采用方括号([])语法:
user[“name”]=”tom”;
使用这种方式还有一个额外的特点,就是可以使用非标识符字符串作为属性名称,例如
标识符中不允许以数字开头或者出现空格,但在方括号([])语法中却可以使用:
user[“my name”]=”tom”;
需要注意,在使用这种非标识符作为名称的属性时,仍然要用方括号语法来引用:
alert(user[“my name”]);
而不能写为:
alert(user.my name);
『肆』 在IE中,如何使用JS删除DOM对象的属性
in判断的是对象的所有属性,包括对象实例及其原型的属性;
而hasOwnProperty则是判断对象实例的是否具有某个属性。
示例代码:
<script type="text/javascript"> function Person(){ } Person.prototype.name = "allen"; var person = new Person(); console.log(person.hasOwnProperty("name")); //false console.log("name" in person); //true console.log(person.name); //"allen" person.name = "justforse"; console.log(person.hasOwnProperty("name")); //true console.log("name" in person); //true console.log(person.name); //"justforuse" delete person.name; console.log(person.hasOwnProperty("name")); //false console.log("name" in person); //true console.log(person.name); //"allen" </script>
以上代码执行的时候,name属性要么是从实例中获取的,要么是来源于原型,所以使用in 来访问 name属性始终返回true;而hasOwnProperty()只在属性存在与对象实例中时才返回true,当删除了实例中的name属性后,就恢复了原型中name属性的连接,所以返回allen。
『伍』 如何用JS删除响应头里面自定义的属性
<div class="div-info" testAttr="myAttr" testAttr2="haha">
</div>
1、js中设置自定义属性。
例如:$(".div-info").attr("testAttr3","houhou");
结果:给div设置了新的自定义属性testAttr3,值为houhou
<div class="div-info" testAttr="myAttr" testAttr2="haha" testAttr3="houhou">
</div>
2、js中获取自定义属性值。
例如:$(".div-info").attr("testAttr");
结果:取到testAttr的值为:myAttr
3、js中修改自定义属性值。
例如:$(".div-info").attr("testAttr","newAttr");
结果:将testAttr的值修改为newAttr
<div class="div-info" testAttr="newAttr" testAttr2="haha">
</div>
4、js中删除自定义属性
来自参考!
『陆』 js移除onmousemove属性
var div = doucment.getElementById('xxx'); //先取得这个DOM
delete div.onmousemove;
不过说实话,一般都是回通过div.onmousermove = null 来进行事件答解除的
『柒』 如何利用JS实现在li中添加或删除class属性
可以使用jQuery的attr方法来实现对某一元素的的class的属性的添加或者删除,attr() 方法设置或返版回被选元素的属性值.根据该方法不权同的参数,其工作方式也有所差异,可以使用removeclass来删除class属性。
工具原料:编辑器、浏览器
1、为li添加class属性:
为某个li元素添加class=“special”的属性
$('li').attr('class','special');
2、删除class属性:
$("li").removeClass("special");
});