Lập trình JavaScript

Nội dung  Giới thiệu về JavaScript  Biến, kiểu dữ liệu, phép toán  Lệnh điều khiển  Popup  Sử dụng các đối tượng

pdf40 trang | Chia sẻ: thuongdt324 | Lượt xem: 443 | Lượt tải: 0download
Bạn đang xem trước 20 trang tài liệu Lập trình JavaScript, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
Đỗ Thanh Nghị dtnghi@cit.ctu.edu.vn Cần Thơ 04-11-2005 Khoa Công Nghệ Thông Tin Trường Đại Học Cần Thơ Lập Trình JavaScript Nội dung  Giới thiệu về JavaScript  Biến, kiểu dữ liệu, phép toán  Lệnh điều khiển  Popup  Sử dụng các đối tượng 2 Printed with FinePrint trial version - purchase at www.fineprint.com  Giới thiệu về JavaScript  Biến, kiểu dữ liệu, phép toán  Lệnh điều khiển  Popup  Sử dụng các đối tượng 3 Giới thiệu về JavaScript 4  JavaScript là gì ?  JavaScript được thiết kế để cùng với HTML tạo trang Web sinh động  JavaScript là ngôn ngữ script, hướng đối tượng, chứa các dòng lệnh thực thi được  JavaScript được viết trực tiếp vào trang HTML  Javascript là ngôn ngữ thông dịch  Javascript khác với Java Printed with FinePrint trial version - purchase at www.fineprint.com Giới thiệu về JavaScript 5  JavaScript dùng làm gì ?  Người thiết kế Web có thể học kỹ năng lập trình đơn giản của JavaScript để viết các trang HTML sinh động  JavaScript xuất những text một cách động cho các trang HTML  JavaScript bắt và xử lý các sự kiện từ giao tiếp của người sử dụng Webbrowser  JavaScript có thể đọc và viết các phần tử cơ bản hay nội dung của trang HTML  JavaScript có thể được sử dụng để kiểm tra dữ liệu trước khi submit  JavaScript có thể cung cấp thông tin về browser  Tạo cookies Ví dụ 1 về JavaScript 6 Printed with FinePrint trial version - purchase at www.fineprint.com Ví dụ 2 về JavaScript 7 Ví dụ 3 về JavaScript 8 Printed with FinePrint trial version - purchase at www.fineprint.com Ví dụ 4 về JavaScript 9 Ví dụ 5 về JavaScript 10 Printed with FinePrint trial version - purchase at www.fineprint.com  Giới thiệu về JavaScript  Biến, kiểu dữ liệu, phép toán  Lệnh điều khiển  Popup  Sử dụng các đối tượng 11 Biến 12  Biến  Chứa dữ liệu  Phân biệt giữa ký tự thường và hoa  Khai báo : var strname = some value strname = some value  Gán giá trị : var strname = "Hege" strname = "Hege"  Phạm vi sử dụng biến : cục bộ và toàn cục Printed with FinePrint trial version - purchase at www.fineprint.com Ví dụ 13 Kiểu dữ liệu 14  Kiểu dữ liệu  Số nguyên : 10, -301, 1974, etc.  Số thực và số chấm động : 13.5, 1.35E1  Luận lý : true, false  Chuỗi : “do thanh nghi”, “sinh nam 1974”, \b, \n, \r, \t, \\ var quote = "He read \"The Cremation of Sam McGee\" by R.W. Service." document.write(quote) Kết quả sẽ là : He read "The Cremation of Sam McGee" by R.W. Service. Printed with FinePrint trial version - purchase at www.fineprint.com Kiểu dữ liệu 15  Kiểu dữ liệu  Mảng : myArray = new Array(10); foo = new Array(5); myArray[0] = 56; myArray[9] = 44; colors = new Array(); colors[99] = "midnightblue"; numberOfElements = myArray.length; Kiểu dữ liệu 16  Kiểu dữ liệu  Đối tượng : Printed with FinePrint trial version - purchase at www.fineprint.com Chuyển kiểu dữ liệu 17  Kiểu dữ liệu  Được chuyển tự động : var answer = 42 answer = "Thanks for all the fish..." x = "The answer is " + 42  Chuyển chuỗi sang số : ParseInt(), ParseFloat() Phép toán 18  Phép toán  Gán : =  Phép tăng hoặc giảm 1 : ++, --  Gán rút gọn : +=, -=, *=, /=, %=  So sánh : ==, !=, , >=  Phép tính số học : +, -, *, /, %  Phép toán luận lý : &&, ||, !,  Phép : (cond) ? Expr1:Expr2 Printed with FinePrint trial version - purchase at www.fineprint.com Phép toán 19 Phép toán 20 Printed with FinePrint trial version - purchase at www.fineprint.com Phép toán 21 Phép toán 22 Printed with FinePrint trial version - purchase at www.fineprint.com Phép toán 23 Định nghĩa hàm 24  Định nghĩa function functionname(var1,var2,...,varX) { some code } Printed with FinePrint trial version - purchase at www.fineprint.com Định nghĩa hàm 25  Ví dụ function total(a,b) { x=a*b return x } product=total(2,3) Định nghĩa hàm 26  Ví dụ function displaymessage() { alert("Hello World!") } <input type="button" value="Click me!" onclick="displaymessage()" > Printed with FinePrint trial version - purchase at www.fineprint.com Định nghĩa hàm 27 Định nghĩa hàm 28 Printed with FinePrint trial version - purchase at www.fineprint.com  Giới thiệu về JavaScript  Biến, kiểu dữ liệu, phép toán  Lệnh điều khiển  Popup  Sử dụng các đối tượng 29 Cấu trúc IF-ELSE 30  Cú pháp if (condition) { statements1 } Hay if (condition) { statements1 } else { statements2 } Printed with FinePrint trial version - purchase at www.fineprint.com Cấu trúc IF-ELSE 31 Cấu trúc IF-ELSE 32 Printed with FinePrint trial version - purchase at www.fineprint.com Cấu trúc lựa chọn switch-case 33  Cú pháp switch(n) { case 1: execute code block 1 break case 2: execute code block 2 break default: code to be executed if n is different from case 1 and 2 } Cấu trúc lựa chọn switch-case 34  Ví dụ theDay=d.getDay() switch (theDay) { case 5: document.write("Finally Friday") break case 6: document.write("Super Saturday") break case 0: document.write("Sleepy Sunday") break default: document.write("I'm looking forward to this weekend!") } Printed with FinePrint trial version - purchase at www.fineprint.com Cấu trúc lựa chọn switch-case 35 Cấu trúc lặp for 36  Cú pháp for (initial-expression; condition; increment-expression) { statements } Ví dụ: var n = 0; for (var i = 0; i < 3; i++) { n += i; alert("The value of n is now " + n); } Printed with FinePrint trial version - purchase at www.fineprint.com Cấu trúc lặp for 37 Cấu trúc lặp for 38 Printed with FinePrint trial version - purchase at www.fineprint.com Cấu trúc lặp while, do-while 39  Cú pháp while (var<=endvalue) { code to be executed } do { code to be executed } while (var<=endvalue) Cấu trúc lặp while, do-while 40  Ví dụ var i=0 while (i<=10) { document.write("The number is " + i) document.write("") i=i+1 } Printed with FinePrint trial version - purchase at www.fineprint.com Cấu trúc lặp while, do-while 41  Ví dụ var i=0 do { document.write("The number is " + i) document.write("") i=i+1 } while (i<0) Cấu trúc lặp while, do-while 42 Printed with FinePrint trial version - purchase at www.fineprint.com Cấu trúc lặp while, do-while 43 break trong cấu trúc lặp 44 Printed with FinePrint trial version - purchase at www.fineprint.com continue trong cấu trúc lặp 45  Giới thiệu về JavaScript  Biến, kiểu dữ liệu, phép toán  Lệnh điều khiển  Popup  Sử dụng các đối tượng 46 Printed with FinePrint trial version - purchase at www.fineprint.com Popup 47  Các lọai  Alert box  Confirm box  Prompt box alert("sometext") 48 Printed with FinePrint trial version - purchase at www.fineprint.com confirm("sometext") 49 prompt("sometext","defaultvalue") 50 Printed with FinePrint trial version - purchase at www.fineprint.com Kết hợp với form 51 Kết hợp với form 52 Printed with FinePrint trial version - purchase at www.fineprint.com  Giới thiệu về JavaScript  Biến, kiểu dữ liệu, phép toán  Lệnh điều khiển  Popup  Sử dụng các đối tượng 53 Đối tượng 54  Các đối tượng  String  Date  Array  Boolean  Math  HTML DOM Printed with FinePrint trial version - purchase at www.fineprint.com Đối tượng string 55 Đối tượng string 56 Printed with FinePrint trial version - purchase at www.fineprint.com Đối tượng string 57  Ví dụ var txt="Hello world!" document.write(txt.length) document.write(txt.toUpperCase()) document.write(txt.substring(4, 8)) document.write(txt.small()) document.write(txt.strike()) document.write(txt.charAt(8)) Đối tượng Date 58 Printed with FinePrint trial version - purchase at www.fineprint.com Đối tượng Date 59 Đối tượng Date 60 Printed with FinePrint trial version - purchase at www.fineprint.com Đối tượng Date 61  Ví dụ var myDate=new Date() myDate.setFullYear(2010,0,14) myDate.setDate(myDate.getDate()+5) myDate.setFullYear(2010,0,14) var today = new Date() if (myDate>today) alert("Today is before 14th January 2010") else alert("Today is after 14th January 2010") Đối tượng Array 62 Printed with FinePrint trial version - purchase at www.fineprint.com Đối tượng Array 63  Ví dụ var mycars=new Array() mycars[0]="Saab" mycars[1]="Volvo" mycars[2]="BMW“ var mycars=new Array("Saab","Volvo","BMW") var mycars=new Array(3) mycars[0]="Saab" mycars[1]="Volvo" mycars[2]="BMW" Đối tượng Array 64  Ví dụ var arr = new Array(3) arr[0] = "Jani" arr[1] = "Tove" arr[2] = "Hege" var arr2 = new Array(3) arr2[0] = "John" arr2[1] = "Andy" arr2[2] = "Wendy" document.write(arr.concat(arr2)) Printed with FinePrint trial version - purchase at www.fineprint.com Đối tượng Array 65  Ví dụ var arr = new Array(6) arr[0] = "Jani" arr[1] = "Hege" arr[2] = "Stale" arr[3] = "Kai Jim" arr[4] = "Borge" arr[5] = "Tove" document.write(arr + "") document.write(arr.sort()) Đối tượng Boolean 66 Printed with FinePrint trial version - purchase at www.fineprint.com Đối tượng Boolean 67  Ví dụ var myBoolean=new Boolean() var myBoolean=new Boolean(0) var myBoolean=new Boolean(null) var myBoolean=new Boolean("") var myBoolean=new Boolean(false) var myBoolean=new Boolean(NaN) var myBoolean=new Boolean(true) var myBoolean=new Boolean("true") var myBoolean=new Boolean("false") var myBoolean=new Boolean("Richard") Đối tượng Math 68 Printed with FinePrint trial version - purchase at www.fineprint.com Đối tượng Math 69 Đối tượng Math 70  Ví dụ Math.E Math.PI Math.SQRT2 Math.SQRT1_2 Math.LN2 Math.LN10 Math.LOG2E Math.LOG10E document.write(Math.round(4.7)) document.write(Math.random()) document.write(Math.floor(Math.random()*11)) document.write(Math.round(-4.60)) Printed with FinePrint trial version - purchase at www.fineprint.com Đối tượng HTML DOM 71 Đối tượng HTML DOM 72 Printed with FinePrint trial version - purchase at www.fineprint.com Đối tượng HTML DOM 73 Printed with FinePrint trial version - purchase at www.fineprint.com Printed with FinePrint trial version - purchase at www.fineprint.com Printed with FinePrint trial version - purchase at www.fineprint.com Printed with FinePrint trial version - purchase at www.fineprint.com