js删除属性,jQuery 删除属性
js 附加属性如何删除
可以用delete命令
比如你附加了s.a = "ABC"。
用 delete s['a']
可以把a和它对应的值都彻底删除
如何用js删除 dom元素的嵌入样式(style)的一个属性
将style中你想删掉的属性设为null应该是可以解决的,Chrome通过
例如:
elem.style["height"] = null;//彻底删除elem的style属性的height值
js用正则表达式删除指定属性
var?source?=?'div?style="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){
return?g1?+?g2.replace(/\s?(.*?):(.*?);/g,function(m,g1,g2){
//?删除b属性和c属性?要改成其他属性可以在这里控制
if(/^(b|c)$/.test(g1))
return?'';
return?m;
})?+?g3;
}));
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中删除自定义属性
来自参考!