js删除元素|js删除属性与增加属性详解

更新时间:2018-11-28    来源:说说    手机版     字体:

【www.bbyears.com--说说】

一.js属性操作归纳

我们在前端开发过程中会遇到各种各样的操作,其中就会涉及到操作属性,下面我就给大家说出JS几种比较常用属性操作,当然我不会以实例去讲解,只是简要的说说用法。

1.通用属性操作:obj.setAttribute(属性的名字)

2.class操作:obj.className=” “;

3.img的src的操作:imgobj.src=” “;

4.input的值操作:inputobj.value

5.其它

二.js属性操作中需要注意的问题

1.JS中不允许出现的特殊字符

2几个兼容问题:js不能直接通过obj.style.float属性,通过IE(styleFloat)、非IE(cssFloat)解决


三,实例(设置DIV的属性  )

setAttribute方法:

 代码如下

var a=document.createElement("div");        //新建一个DIV
a.id="div1";      //给新加的DIV命名
a.style.setAttribute("zIndex",2);     //设置DIV叠放次序
a.style.setAttribute("textAlign",Dalign);   //对齐方式
a.style.setAttribute("border","#e6e7e8 1px solid");   //边框颜色
a.style.width=divwidth;      //DIV宽度
a.style.height=Dheight;      //DIV高度
a.setAttribute("position","absolute");
a.style.backgroundColor=Dbgcolor;      //DIV背景
a.setAttribute("z-index","2");      //DIV叠放次序
a.style.top = divtop+"px";      //DIV上边距
a.style.left = divleft+"px";      //DIV左边距   
a.setAttribute("innerHTML",info10[0].firstChild.data+"
"+info11[0].firstChild.data);
document.body.appendChild(a);       //新建DIV结束

隐藏div:document.getElementById(“啊”).style.display="none"  //block 出现

document.getElementById(“啊”).style.disabled="true" 

document.getElementById(“啊”).style.readOnly="true" 

根据上面实例总结

1:遍历对象属性可以通过(var attrName in obj)的方式进行遍历

2:判断某个对象是不是拥有某个属性可以通过attrName in obj的方式来判断,如果没有这个属性会返回undefined,对象可以为某个对象增加某个属性,但是并不为这个属性设值

3:判断某个对象的某个属性是不是在自己的构造函数中定义的而不是通过继承得来的,可以通过函数obj.hasOwnProperty(attrName).

4:判断某个属性可不可以通过for/in的方式遍历可以通过obj.propertyInEnumerable(attrName),但是该属性必须是通过非继承的方式得来的。

本文来源:http://www.bbyears.com/zhufuduanxin/46138.html