Javascript-use createelement to dynamically create an HTML object. Pass a value in the form

Source: Internet
Author: User

From: http://blog.csdn.net/fxh_hua/article/details/4433682 to find your own convenience

 

1. Create a link

 

<Body>
<Script language = "JavaScript">
VaR o = Document. Body;
// Create a link
Function createa (URL, text)
{
VaR A = Document. createelement ("");
A. href = URL;
A. innerhtml = text;
A. style. color = "red ";
O. appendchild ();
}
Createa ("http://www.hao123.com/", "hao ");
</SCRIPT>
</Body>

 

 

2. Create a div <Body>
<Script language = "JavaScript">
VaR o = Document. Body;
// Create a div
Function creatediv (text)
{
VaR DIV = Document. createelement ("Div ");
Div. innerhtml = text;
Div. style. Background = "red ";
O. appendchild (DIV );
}
Creatediv ("Div ");
</SCRIPT>
</Body> 3. Create a form item <Body>
<Script language = "JavaScript">
VaR o = Document. Body;
// Create a form item
Function createinput (stype, svalue)
{
VaR input = Document. createelement ("input ");
Input. type = stype;
Input. value = svalue;
O. appendchild (input );
}
Createinput ("button", "AA ");
</SCRIPT>
</Body> 4. Create a table <Body>
<Script language = "JavaScript">
VaR o = Document. Body;
// Create a table
Function createtable (W, H, R, c)
{
VaR table = Document. createelement ("table ");
VaR tbody = Document. createelement ("tbody ");
Table. width = W;
Table. Height = h;
Table. Border = 1;
For (VAR I = 1; I <= r; I ++)
{
VaR TR = Document. createelement ("TR ");
For (VAR j = 1; j <= C; j ++)
{
VaR TD = Document. createelement ("TD ");
TD. innerhtml = I + "" + J;
// TD. appendchild (document. createtextnode (I + "" + j ));
TD. style. color = "# ff0000 ";
Tr. appendchild (TD );
}
Tbody. appendchild (TR );
}
Table. appendchild (tbody );
O. appendchild (table );
}
Createtable (270,270 );
</SCRIPT>
</Body> 4. Create a button, text box, and input values in the form (Htmlpage1.htm)  <SCRIPT type = "text/JavaScript">
VaR Index = 2; Function addele (){

VaR TXT = Document. createelement ("input ");
TXT. setattribute ("type", "input ");
TXT. setattribute ("ID", "TXT" + index ); VaR BTN = Document. createelement ("input ");
BTN. setattribute ("type ","'");
BTN. setattribute ("ID", "BTN" + index );
BTN. setattribute ("value", "value ");
BTN. setattribute ("onclick", "OpenWindow ()"); VaR BR = Document. createelement ("Br "); Document. getelementbyid ("content"). appendchild (txt );
Document. getelementbyid ("content"). appendchild (BTN );
Document. getelementbyid ("content"). appendchild (BR ); Index ++; } Function OpenWindow (){
VaR returnval = Window. showmodaldialog ("htmlpage2.htm ","","");
VaR srcid = Window. event. srcelement. ID; // the object that triggers the event.
VaR ind = srcid. substr (3, srcid. Length );
For (var I in returnval ){
Document. getelementbyid ("TXT" + IND). Value + = returnval [I] + "";
}
}

</SCRIPT>Htmlpage2.htm <Script Type = "text/JavaScript">

Function Sub (){
VaR arr = Document. getelementsbytagname ("input ");
Var val = new array ();
For (var I in ARR ){
If (ARR [I]. type = "checkbox "){
If (ARR [I]. Checked ){
Val [Val. Length] = arr [I]. value;
}
}
}
Window. returnvalue = val;
Window. Close ();
}

</SCRIPT>

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.