js控制div及相关属性

来源:互联网 发布:php企业源码 编辑:程序博客网 时间:2024/06/05 15:17

动态创建DIVvar div = document.createElement("div"); 

设置div属性及样式等:

div.title="this is a new div."; 

       div.class = "newDivClass";   

div.innerHTML = "Test create a div element!";

div.style.styleFloat="left"; 
div.style.overflow="hidden"; 
div.style.marginLeft="8px"; 
div.style.marginTop="10px"; 
div.style.width="320px"; 
div.style.height="250px"; 
div.style.backgroundRepeat="no-repeat"; 

div.style.backgroundImage="url(image/bgred.jpg)"  

DIV添加到bodydocument.body.appendChild(div );

设置Attribute属性:  

添加,设置:

(1).       div.setAttribute("ok","ok?");

html:  <div id="id" style="background-color: red;" ok="ok?"/>

(2).       var ok = document.createAttribute("name");

div.setAttributeNode(ok);

html:  <div id="idstyle="background-color: red;name=""/>

获取:

(1).div.getAttribute("ok");//ok?

(2)var v = div.getAttributeNode("ok").value;// div.getAttributeNode("ok")获取的是//AttributeNode对象。

删除

(1).document.getElementById("id").removeAttribute("ok");//ok为属性名。

(2).document.getElementById("id").removeAttributeNode(ok);//okAttributeNode对象。

原创粉丝点击