Javascript DOM programming example (smart podcast learning)

Source: Internet
Author: User

I also explained a lot about how to learn Dom. I think it is really beneficial.
The following are some examples that I think are better.
1. Level-2 linkage drop-down menu (level-1 is a province, level-2 is a city, and level-1 is required to select a province, level-2 cities correspond to appear)
Step: 1) Basic Framework (HTML): There are two check boxes, and there should be no options in the second check box. Note that the first drop-down box will be used as the event source, the onchange method is used to stimulate the function implementation in JavaScript. CopyCode The Code is as follows: <XMP>
<Select id = "Province" onchange = "selcity ()">
<Option> -- select a province -- </option>
<Option> Beijing </option>
<Option> Tianjin </option>
<Option> Hubei </option>
<Option> Shandong </option>
<Option> Hebei </option>
</SELECT>
<Select id = "city">
<Option> -- select a city -- </option>
</SELECT>
</XMP>

2) There is no need to modify the page. You just need to display one in the drop-down menu.
3) JavaScript is used to implement the function. If you want to select a province from the first drop-down list, the corresponding city will be directly displayed, first, we thought of the map set container, but the javascrip does not have a map set. Then we thought of an array container and finally decided to use a two-dimensional array to help us implement this function.Copy codeThe Code is as follows: <SCRIPT type = "text/JavaScript">
Function selcity ()
{
VaR arr = [["-- select city --"], ["Haidian District", "Chaoyang District", "Dongcheng District", "Xicheng district", "Xuanwu District"],
["Heping District", "Hebei District", "Hedong district", "Hexi District", "Nankai district"],
["Wuhan", "Yichang", "Xiaogan", "Xiangfan", "Jingzhou"],
["Jinan", "Qingdao", "Yantai", "Weihai", "Rizhao"],
["Shijiazhuang", "Baoding", "Zhangjiakou", "Langfang", "Handan"];
VaR selpnode = Document. getelementbyid ("Province ");
VaR selcnode = Document. getelementbyid ("city ");
VaR citys = arr [selpnode. selectedindex];
Selcnode. Options. Length = 0;
For (VAR x = 0; x <Citys. length; X ++)
{
VaR optnode = Document. createelement ("option ");
Optnode. innertext = citys [x];
Selcnode. appendchild (optnode );
}
}
</SCRIPT>

Summary: Remember to clear the array each time.
2. email list
Idea: 1) first think of the overall framework, you will surely think of using tables for formatting, and then using tables, rows, and cells to implement relevant functions.
2) CSS is mainly used to format the table, followed by selecting and displaying different results for the two rows. It is implemented through dynamic modification of classname.
Due to the length, some HTML and CSS code is relatively simple and will not be ugly.
3) JavaScript to implement various functions. The Code is as follows: Copy code The Code is as follows: <SCRIPT>
VaR color = "";
Function getstyle ()
{
VaR tablenode = Document. getelementsbytagname ("table") [0];
VaR arr = tablenode. Rows;
For (VAR x = 0; x <arr. length; X ++)
{
If (X % 2)
Arr [X]. classname = "one ";
Else
Arr [X]. classname = "two ";
VaR tdnode0 = arr [X]. cells [0];
Tdnode0.align = "center ";
Arr [X]. onmouseover = function ()
{
Color = This. classname; // record the original value, and the original color can be returned after the mouse is clicked.
This. classname = "checked ";
}
Arr [X]. onmouseout = function ()
{
This. classname = color;
}
}
}
// We need to select all to implement the same function, so we can identify the node by passing values.
Function allcheck (INDEX)
{
VaR allnode = Document. getelementsbyname ("all") [Index];
VaR checknodes = Document. getelementsbyname ("mail ");
For (VAR x = 0; x <checknodes. length; X ++)
{
Checknodes [X]. Checked = allnode. checked;
}
}
Function checkbut (Num)
{
VaR mailnodes = Document. getelementsbyname ("mail ");
For (VAR x = 0; x <mailnodes. length; X ++)
{
If (Num> 1)
Mailnodes [X]. Checked =! (Mailnodes [X]. Checked); // returns the status of the user to the user for reverse selection.
Else
Mailnodes [X]. Checked = num;
}
}
Function del ()
{
VaR B = Window. Confirm ("are you sure you want to delete the selected email? ");
If (! B)
Return;
VaR mailnodes = Document. getelementsbyname ("mail ");
VaR arr = new array ();
VaR Pos = 0;
For (VAR x = 0; x <mailnodes. length; X ++)
{
If (mailnodes [X]. Checked)
{
VaR trnode = mailnodes [X]. parentnode. parentnode;
Arr [POS ++] = trnode;
}
}
For (VAR x = 0; x <arr. length; X ++)
{
Arr [X]. parentnode. removechild (ARR [x]);
}
Getstyle ();
}
Window. onload = getstyle;
</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.