Dynamically create a div using Js

Source: Internet
Author: User

This is the original CSS style.
. Item {float: left; overflow: hidden; margin-left: 8px; margin-top: 10px; width: 320px; height:
250px; background-repeat: no-repeat; background-image: url (../images/bgred.jpg )}
. Curve {position: relative; width: 320px; height: 250px; z-index: 1; left: 75px; top:-40px ;}
The code for dynamically creating a DIV is as follows:
For (j = 0; j * 8 <str. length; j ++)
{
Var mydiv = window. frames ["displayFrame" comment .doc ument. createElement ("div ");
Mydiv. setAttribute ("id", "itemdiv" + j );
Mydiv. style. styleFloat = "left ";
Mydiv. style. overflow = "hidden ";
Mydiv. style. marginLeft = "8px ";
Mydiv. style. marginTop = "10px ";
Mydiv. style. width = "320px ";
Mydiv. style. height = "250px ";
Mydiv. style. backgroundRepeat = "no-repeat ";
Mydiv. style. backgroundImage = "url (image/bgred.jpg )"
Window. frames ["displayFrame" mirror.doc ument. body. appendChild (mydiv );
Var curvediv = window. frames ["displayFrame" comment .doc ument. createElement ("div ");
Curvediv. setAttribute ("id", "curvediv" + j );
Curvediv. style. position = "relative ";
Curvediv. style. zIndex = 1;
Curvediv. style. left = "75px ";
Curvediv. style. top = "-40px ";
Curvediv. style. width = "320px ";
Curvediv. style. height = "250px ";
Window. frames ["displayFrame" mirror.doc ument. getElementById ("divitem" + j). appendChild (curvediv );
}
Add the div element to HTML.
You can also define a SPAN in HTML.
Window. frames ["displayFrame" mirror.doc ument. getElementById ("spanId"). appendChild (mydiv );
Window. frames ["displayFrame" mirror.doc ument. body. appendChild (mydiv );
Both IE and Firefox support.
Note that this CSS Element
Floating effect: float: left
Code in IE: mydiv. style. styleFloat = "left ";
The code in Firefox is: mydiv.style.css Float = "left ";
Other elements such:
In CSS writing, it is generally: margin-left: 8px
In addition, we need to remove-: mydiv. style. marginLeft = "8px ";
I have not carefully studied the case sensitivity issue.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.