"JS learning" JS Basic 2

Source: Internet
Author: User

One, operator

(1) Arithmetic: + Plus, minus, * multiply,/divide,% take surplus

Example: Interlaced color, seconds turn time

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title> Interlaced </title>
<script>
Window.onload = function ()
{
var ali=document.getelementsbytagname (' Li ');

for (Var i=0;i<ali.length;i++)
{
if (i%2==0)
{
Ali[i].style.background= ' #CCC ';
}
Else
{
Ali[i].style.background= ";
}
}
};

</script>


<body>

<ul>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</body>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title> sec-Turn time </title>
<script>
var s=156;

Alert (parseint (S/60) + ' +s%60+ ' seconds ');
</script>

<body>
</body>

(2) Assignment Value: =, + =,-=, *=,/=,%=

Like what

i=i+1;

i++;

I+=1;

i=i+3;

i+=3;

(3) Relationship:<, >, <=, >=, = =, = =,! = (unequal),!==

(4) Logic:&& with, | | Or! Whether

Example: Full selection and inverse selection

(5) Arithmetic priority: parentheses

Second, the program flow control

(1) Judgment: If,switch,?:

Switch syntax:

Switch (variable)

{

Case value 1:

Statement 1;

Break

Case Value 2:

Statement 2;

Break

......

Default

Statement N;

}

?: syntax-ternary operator/three-mesh operator

In the IF syntax

if (condition)

{

Statement 1;

}else

{

Statement 2;

}

Equivalent to in the?: Grammar

Condition? Statement 1: Statement 2

(2) Cycle: While,for

(3) Jump: break,continue

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>break and Continue</title>
<script>
for (i=0;i<5;i++)
{
if (i==2)
{
break;//Cycle Interrupt
}
alert (i);
}
</script>


<body>
</body>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>break and Continue</title>
<script>
for (i=0;i<5;i++)
{
if (i==2)
{
break;//Cycle Interrupt
continue;//just interrupted this cycle.
}
alert (i);
}
</script>


<body>
</body>

Iii. what is true and what is False

True: true, not 0 digits, non-empty strings, non-empty objects (such as document, array)

False: False, number 0, empty string, empty object (null), undefined

Four, Json

What is JSON: a subset of JavaScript-based.

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>Json</title>
<script>
/*var a=12;
var b=5;
var c= ' abc '; */

var json={a:12, B:5, C: ' abc '};

alert (JSON.A);
json.b++;
alert (JSON.B);
</script>


<body>
</body>

(2) JSON and array

Loops: In general, arrays are used from 0-length loop statements

Encountered JSON with for in

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>json and Arrays </title>
<script>
var json={a:12, B:5, c:7};
var arr=[12,5,7];

alert (JSON.A);
Alert (json[' a ']);//jons's subscript is a string
Alert (arr[0]);//The subscript of an array is a number

alert (arr.length);//Output 3
alert (json.length);//output Undefined,json no length

Cycle
/*for (Var i=0;i<arr.length;i++)
{
Alert (' +i+ ' element: ' +arr[i]);
}*/

/*for (var i in arr)
{
Alert (' +i+ ' element: ' +arr[i]);
}*/

for (var i in JSON)
{
Alert (' +i+ ' element: ' +json[i]);
}
</script>


<body>
</body>

"JS learning" JS Basic 2

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.