Home > Web Front-end > JS Tutorial > body text

JavaScript dynamically creates div attributes and styles sample code_javascript skills

WBOY
Release: 2016-05-16 17:20:40
Original
1326 people have browsed it
1. Create a div element:

Javascript code
Copy code The code is as follows:


functioncreateElement(){
varcreateDiv=document.createElement("div");
createDiv.innerHTML="Testcreateadivelement! ";
document.body.appendChild(createDiv);
}



functioncreateElement(){ < !-- no "document" if not body-->
}




2. Create div attributes:

Javascript code


Copy code
The code is as follows: functioncreateElement(){
varcreateDiv=document.createElement("div");
createDiv.title="thisisanewdiv.";
createDiv.id="newDivId";
createDiv.class="newDivClass";
createDiv.innerHTML="Testcreateadivelement!";
document.body.appendChild(createDiv);
}



functioncreateElement(){
varcreateDiv=document.createElement("div");
createDiv.title="thisisanewdiv.";
createDiv .id="newDivId";
createDiv.class="newDivClass";
createDiv.innerHTML="Testcreateadivelement!";
document.body.appendChild(createDiv);

createDiv.className = "name file_col";
}




3. Create div Style:

Javascript code


Copy code functioncreateElement(){
varcreateDiv=document.createElement("div");
createDiv.style.background="pink";
createDiv.style.border="1pxsolidred";
createDiv.style.width="50px";
createDiv.style.height="50px";
createDiv.innerHTML="Testcreateadivelement!" ;
document.body.appendChild(createDiv);
}


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template