JS Basic Exercise

Source: Internet
Author: User

Recently in the learning front, of course, including js,css,html what, in the lectures, some of the small exercises, recorded:

Example one:

--tab Tabs--

<script type= "Text/javascript" >
Window.onload=function () {
var ali=document.getelementsbytagname ("Li");
var adiv=document.getelementsbytagname ("div");
var i=0;
for (i=0;i<ali.length;i++)
{
Ali[i].index=i;
Ali[i].onclick=function ()
{
for (i=0;i<ali.length;i++)
{
Ali[i].classname= "";
Adiv[i].style.display= ' None ';
}
This.classname= ' active ';
Alert ("This is the first" + This.index + "buttons");
adiv[this.index].style.display= ' block ';
};
}
};
</script>

The corresponding CSS, as well as the DIV:

<style type= "Text/css" > ul{margin:0px; padding:0px; list-style:none;} li{float:left; height:34px; width:80px; line-height:34px; Text-align:center; Background: #000; Color: #F00; border: #CCC solid 1px;} li:hover{text-decoration:underline; border: #999 solid 1px;}. active {float:left; height:34px; width:80px; line-height : 34px; Text-align:center; Background: #000; Color: #CCC; border: #CCC solid 1px;} div{background: #CCC; margin-left:2px; width:320px; height:200px; border: #333 solid 1px; display:none;}

</style>

<ul> <li > Miao tang </li> <li> Miao Cha Museum </li> <li>js Foundation Lesson </li> <li> More << </li> </ul>

<div style= "Display:block;" > Miao tang </div> <div> Miao Cha Museum </div> <div> Foundation Class </div> <div> more ...</div>

Example two:

--To achieve a full selection--

<script type= "Text/javascript" >
Window.onload=function () {
var Cbtn=document.getelementbyid ("btn");
var ipt=document.getelementsbytagname ("input");
var j=0;
Cbtn.onclick=function () {
for (Var j=0;j<ipt.length;j++) {
Ipt[j].checked=true;
};
}
};
</script>

This JS does not debug still have the problem!!

<p id= "BTN" > Full selection </p>
<input type= "checkbox"/><br/>
<input type= "checkbox"/><br/>
<input type= "checkbox"/><br/>
<input type= "checkbox"/><br/>

Summarize the points of knowledge:

Event extraction

If judgment

For loop

Select All function

getElementsByTagName

This

Add an ordinal to an element (index)

Options tab

In fact, this is the most basic and simple example, playing a good foundation, skilled use, in the future to strengthen the study in order to have a qualitative breakthrough.

JS Basic Exercise

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.