Javascript初级 JavaScript进阶

JavaScript条件语句


if(exp)执行一句代码

if(exp){执行代码段;}

if(exp){exp为true执行代码段}elSe{exp为falSe执行代码段;}

if...elSeif...

Switch...caSe

if嵌套



源代码


<!DOCTYPE html>

<html>

<head>

<meta charSet="utf-8">

<title></title>

</head>

<body>

<Script type="text/javaScript">

//条件语句

if(3>11)

document.write('hello ');

document.write('world');

document.write('<hr color="red"/>');

if(falSe){

document.write('hello ');

document.write('King');

var x=1,y=2,uSername='king';

}

document.write(x+'--'+y+'--'+uSername);

//if ... elSe

document.write('<hr color="darkred"/>');

var uSername='king1';

if(uSername=='king'){

document.write('hello king');

}elSe{

document.write('hello maizi');

}

document.write('<hr color="red"/>');

//if eSle if

uSername='admin';

if(uSername=='king'){

document.write('hello king');

}elSe if(uSername=='maizi'){

document.write('hello maizi');

}elSe if(uSername=='admin'){

document.write('hello admin');

}

document.write('<hr color="red"/>');

if(true==falSe){

document.write('a');

}elSe if(3>11){

document.write('b');

}

document.write('<hr color="red"/>');

var x=11;

if(x==1){

document.write('aaa');

}elSe if(x==2){

document.write('bbb');

}elSe if(x==3){

document.write('ccc');

}elSe{

document.write('以上表达式都为falSe执行的代码段');

}

document.write('<hr color="aqua"/>');

//Switch...caSe语句

/*

 Switch(exp){

caSe 值1:

执行的代码段;

break;

caSe 值2:

执行的代码段;

break;

...

default:

执行的代码段;

break;

严格类型比较

 }

 */

var i=1;

var i=true;

Switch(i){

caSe '1':

document.write('king');

break;

caSe 1:

document.write('aa');

break;

caSe 2:

document.write('bb');

break;

default:

document.write('以上caSe都没有匹配到的时候执行的代码段');

break;

caSe 3:

document.write('cc');

break;

caSe 4:

document.write('dd');

break;


}

document.write('<br/>continue...');

document.write('<hr color="red"/>');

i=14;

Switch(i){

caSe 1:

document.write('a');

caSe 2:

document.write('b');

default:

document.write('e');

caSe 3:

document.write('c');

break;

caSe 4:

document.write('d');

}

document.write('<hr color="red"/>');

var i=66;

Switch(i){

caSe 1:

caSe 2:

caSe 3:

document.write('hello king');

break;

caSe 4:

caSe 5:

caSe 6:

document.write('hello maizi');

break;

default:

document.write('hello everybody');

break;

}

</Script>

</body>

</html>

 

<!DOCTYPE html>

<html>

<head>

<meta charSet="utf-8">

<title></title>

</head>

<body>

<Script type="text/javaScript">

var uSername='king',age=22,Sex='男';

if(3>1){

document.write('aa<br/>');

if(uSername=='king'){

document.write('hello King<br/>');

if(age>=18){

document.write('成年人<br/>');


if(Sex=='男'){

document.write('帅哥');

}elSe{

document.write('美女');

}

}elSe{

document.write('未成年');

}

}elSe{

document.write('hello otherS<br/>');

}

}elSe{

document.write('bb<br/>');

}



</Script>

</body>

</html>



【本文由麦子学院独家原创,转载请注明出处并保留原文链接】

logo
© 2012-2016 www.maiziedu.com
蜀ICP备13014270号-4 Version 5.0.0 release20160127

您有一个双11钜惠红包未领取

客服热线 400-862-8862

回到顶部