1.JS与HTML的结合方式
<!DOCTYPE html>
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> Title</ title>
< script src = " js/a.js" > </ script>
</ head>
< body>
< script>
alert ( "奥力给的猛" ) ;
</ script>
</ body>
</ html>
a.js
alert("12345");
2.JS变量
<!DOCTYPE html>
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> Title</ title>
< script>
var num = 1 ;
var num1 = 2 ;
var num2 = NaN ;
document. write ( num+ "---" + typeof ( num) + "<br>" ) ;
document. write ( num1+ "---" + typeof ( num1) + "<br>" ) ;
document. write ( num2+ "---" + typeof ( num2) + "<br>" ) ;
var str1= "abc" ;
var str2= "qwe" ;
var str3= "asd" ;
document. write ( str1+ "---" + typeof ( str1) + "<br>" ) ;
document. write ( str2+ "---" + typeof ( str2) + "<br>" ) ;
document. write ( str3+ "---" + typeof ( str3) + "<br>" ) ;
var bool = true ;
document. write ( bool+ "---" + typeof ( bool) + "<br>" ) ;
var obj1 = null ;
var obj2 = undefined;
var obj;
document. write ( obj1+ "---" + typeof ( obj1) + "<br>" ) ;
document. write ( obj2+ "---" + typeof ( obj2) + "<br>" ) ;
document. write ( obj+ "---" + typeof ( obj) + "<br>" ) ;
</ script>
</ head>
< body>
</ body>
</ html>
3.JS一元运算符
<!DOCTYPE html>
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> Title</ title>
< script>
var num = 3 ;
var a = num++ ;
document. write ( a+ "<br>" ) ;
document. write ( num) ;
document. write ( "<br>" )
var b = + "1234"
document. write ( typeof ( b) + "-----" + b+ "<br>" ) ;
var aa = + "abc"
document. write ( typeof ( aa) + "-----" + aa+ "<br>" ) ;
var flag = + true ;
var fla = + false ;
document. write ( typeof ( flag) + "-----" + flag+ "<br>" ) ;
document. write ( typeof ( fla) + "-----" + fla+ "<br>" ) ;
</ script>
</ head>
< body>
</ body>
</ html>
4.算数运算符
<!DOCTYPE html>
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> Title</ title>
< script>
var a = 2 ;
var b = 3 ;
document. write ( a+ b+ "<br>" ) ;
document. write ( a- b+ "<br>" ) ;
document. write ( a* b+ "<br>" ) ;
document. write ( a/ b+ "<br>" ) ;
document. write ( a% b+ "<br>" ) ;
</ script>
</ head>
< body>
</ body>
</ html>
5.比较运算符
<!DOCTYPE html>
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> Title</ title>
< script>
document. write ( ( 3 > 4 ) + "<br>" ) ;
document. write ( ( "abc" > "aab" ) + "<br>" ) ;
document. write ( ( "123" < 222 ) + "<br>" ) ;
document. write ( ( "123" === 123 ) + "<br>" ) ;
</ script>
</ head>
< body>
</ body>
</ html>
6.逻辑运算符
<!DOCTYPE html>
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> Title</ title>
< script>
var num = 3 ;
var num1 = 0 ;
var num2 = NaN ;
document. write ( ! ! num+ "<br>" ) ;
document. write ( ! ! num1+ "<br>" ) ;
document. write ( ! ! num2+ "<br>" ) ;
document. write ( "-----------------------" + "<br>" ) ;
var str = "" ;
var str1 = "avc" ;
document. write ( ! ! str+ "<br>" ) ;
document. write ( ! ! str1+ "<br>" ) ;
document. write ( "-----------------------" + "<br>" ) ;
var s = null ;
var ss;
document. write ( ! ! s+ "<br>" ) ;
document. write ( ! ! ss+ "<br>" ) ;
document. write ( "-----------------------" + "<br>" ) ;
var date = new Date ( ) ;
document. write ( ! ! date+ "<br>" ) ;
</ script>
</ head>
< body>
</ body>
</ html>
7.三元运算符
<!DOCTYPE html>
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> Title</ title>
< script>
var a = 3 ;
var b = 4 ;
document. write ( a> b? 3 : 5 ) ;
</ script>
</ head>
< body>
</ body>
</ html>
8.JS特殊语法
<!DOCTYPE html>
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> Title</ title>
< script>
function fun ( ) {
b= 4 ;
}
fun ( ) ;
alert ( b) ;
</ script>
</ head>
< body>
</ body>
</ html>
9.流程控制语句—switch
<!DOCTYPE html>
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> Title</ title>
< script>
var a = 1 ;
switch ( a) {
case 1 :
alert ( "number" ) ;
break ;
case a:
alert ( "string" ) ;
break ;
case null :
alert ( "null" ) ;
break ;
case undefined:
alert ( "undefind" ) ;
}
</ script>
</ head>
< body>
</ body>
</ html>
10.流程控制语句—while
<!DOCTYPE html>
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> Title</ title>
< script>
var sum = 0 ;
var num = 1 ;
while ( num<= 100 ) {
sum+= num;
num++ ;
}
alert ( sum) ;
</ script>
</ head>
< body>
</ body>
</ html>
11.流程控制语句—for
<!DOCTYPE html>
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> Title</ title>
< script>
var sum = 0 ;
for ( var i= 1 ; i<= 100 ; i++ ) {
sum+= i;
}
alert ( sum) ;
</ script>
</ head>
< body>
</ body>
</ html>
12.输出乘法表
<!DOCTYPE html>
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> Title</ title>
< style>
td {
border : 1px solid;
}
</ style>
< script>
document. write ( "<table align='center'>" )
for ( var i = 1 ; i<= 9 ; i++ ) {
document. write ( "<tr>" )
for ( var j = 1 ; j<= i; j++ ) {
document. write ( "<td>" )
document. write ( i+ " * " + j+ " = " + ( i* j) + " " ) ;
document. write ( "</td>" )
}
document. write ( "</tr>" )
document. write ( "<br>" )
}
</ script>
</ head>
< body>
</ body>
</ html>
13.对象—function
<!DOCTYPE html>
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> function对象</ title>
< script>
function fun1 ( a, b) {
alert ( a+ b) ;
}
fun1 ( 1 , 2 ) ;
document. write ( fun1. length) ;
var fun2 = function ( a, b) {
alert ( a+ b) ;
}
fun2 ( 2 , 4 ) ;
function add ( ) {
var sum= 0 ;
for ( var i= 0 ; i< arguments. length; i++ ) {
sum += arguments[ i] ;
}
alert ( sum) ;
}
add ( 2 , 3 , 4 , 5 ) ;
</ script>
</ head>
< body>
</ body>
</ html>
14.对象—Array
<!DOCTYPE html>
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> Array对象</ title>
< script>
var arr1 = new Array ( 1 , 2 , 3 ) ;
var arr2 = new Array ( 6 ) ;
var arr3 = [ 4 , 5 , 6 , "奥力给" ] ;
document. write ( arr1+ "<br>" ) ;
document. write ( arr2+ "<br>" ) ;
document. write ( arr3+ "<br>" ) ;
document. write ( arr1. join ( "。" ) + "<br>" ) ;
arr1. push ( 19 ) ;
document. write ( arr1+ "<br>" ) ;
</ script>
</ head>
< body>
</ body>
</ html>
15.对象—Data
<!DOCTYPE html>
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> Data对象</ title>
< script>
var data = new Date ( ) ;
document. write ( data+ "<br>" ) ;
document. write ( data. toLocaleString ( ) + "<br>" ) ;
document. write ( data. getTime ( ) + "<br>" ) ;
</ script>
</ head>
< body>
</ body>
</ html>
16.对象—Math
<!DOCTYPE html>
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> Math对象</ title>
< script>
document. write ( Math. PI + "<br>" ) ;
document. write ( Math. random ( ) + "<br>" ) ;
document. write ( Math. ceil ( 3.14 ) + "<br>" ) ;
document. write ( Math. floor ( 3.14 ) + "<br>" ) ;
document. write ( Math. round ( 3.14 ) + "<br>" ) ;
var num = Math. floor ( Math. random ( ) * 100 ) + 1 ;
document. write ( num) ;
</ script>
</ head>
< body>
</ body>
</ html>
17.对象—正则表达式
<!DOCTYPE html>
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> 正则表达式</ title>
< script>
var reg = new RegExp ( "^\\w{6,12}$" ) ;
var reg2 = /^\w{6,12}$/ ;
var reg3 = /^\d{6,12}$/ ;
var username = "zhangsan" ;
var id= "123456" ;
var b1 = reg2. test ( username) ;
var b2 = reg3. test ( id) ;
alert ( b1) ;
alert ( b2) ;
</ script>
</ head>
< body>
</ body>
</ html>
18.对象—Global
<!DOCTYPE html>
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< title> Global对象</ title>
< script>
var str = "奥力给的猛" ;
var encode = encodeURI ( str) ;
document. write ( encode+ "<br>" ) ;
var decode = decodeURI ( encode) ;
document. write ( decode+ "<br>" ) ;
var str1 = "34531" ;
var number = parseInt ( str1) ;
document. write ( number+ "<br>" )
var a = NaN ;
document. write ( isNaN ( a) + "<br>" ) ;
var js = "alert(123)" ;
eval ( js) ;
</ script>
</ head>
< body>
</ body>
</ html>