Professional Documents
Culture Documents
4
SET STYLES FOR ELEMENTS ID
Implementation steps
Step 1. Use ID to access elements
Step 2. Use the style attribute to set the style for that
element
5
DEMO
Xem CSS
6
DEMO
<html >
<head>
<script type="text/javascript">
function xanhButtonClick() {
var pText = document.getElementById("pText");
pText.style.color = "blue";
}
function doButtonClick() {
var pText = document.getElementById("pText");
pText.style.color = "red";
}
</script>
</head>
<body>
<p id="pText">Thiet lap style bang ID</p>
<button onclick="xanhButtonClick();">Xanh</button>
<button onclick="doButtonClick();">Do</button>
</body>
</html>
7
SETS THE STYLE FOR A GROUP OF
ELEMENTS
The problem arises:
Want to change styles for multiple elements
<p id = "pText">Thiet lap style cho nhieu thanh phan</p>
<p id = "p1">Thanh phan 1</p>
<p id = "p2">Thanh phan 2</p>
<p id = "p3">Thanh phan 3</p>
Solution:
Use the getElementsByName or
getElementsByTagName method to get a group of
elements
Use For loop to iterate over elements
var pAr = document.getElementsByTagName("p");
for (var i = 0; i < pAr.length; i++) {
pAr[i].style.color = "blue";
}
8
IMAGE ROLLOVER
9
ROLLOVER
10
DEMO ROLLOVER
<html>
<head>
<script type="text/javascript">
function onMouseOverEvent() {
document.img_hoa.src = "hoaover.jpg";
}
function onMouseOutEvent() {
document.img_hoa.src = "hoaout.jpg"; Truy cập đến phần
} tử bằng attribute
</script> name
</head><body>
<img name="img_hoa" src="hoaout.jpg"
onmouseover="onMouseOverEvent();"
onmouseout="onMouseOutEvent();" />
</body>
</html>
11
THE PROBLEM ARISES WHEN PERFORMING
ROLLOVER
Problem: The first time you hover over an image, it
takes a while for the image to load even though the
page has been open for a long time.
Cause: When the user hovers over the mouse, the
new image is loaded
Solution: Load the image first
Technique:
Create an image object for each image you want to load first
Assign the path of the image to the src attribute of that image
object
Explain:
When encountering the command
imageObject.src=“link_cua_anh”, the image is loaded
underground, the computer continues to execute the commands
after this command.
This solution does not make website loading slow
13
DEMO LOAD IMAGE
<html><head>
<script type="text/javascript">
var hoaover = new Image();
var hoaout = new Image();
function loadAnh() {
hoaover.src = "hoaover.jpg";
hoaout.src = "hoaout.jpg";
}
function onMouseOverEvent() {
document.img_hoa.src = hoaover.src
}
function onMouseOutEvent() {
document.img_hoa.src = hoaout.src
}
</script></head>
<body onload = "loadAnh();">
<img name="img_hoa" src="hoaout.jpg"
onmouseover="onMouseOverEvent();"
onmouseout="onMouseOutEvent();" />
</body></html>
14
SLIDE SHOW
15
SLIDE SHOW
16
DEMO
var anhAr = [];
var currentIndex=0;
function loadAnh() {
for (var i = 0; i < 6; i++) {
anhAr[i] = new Image();
anhAr[i].src = "anh"+i+".jpg";
}
}
function next() {
if (currentIndex < 4) {
currentIndex++;
document.getElementById("hoa").src = anhAr[currentIndex].src;
}
}
function back() {
if (currentIndex > 0) {
currentIndex--;
document.getElementById("hoa").src = anhAr[currentIndex].src;
}
}
17
DEMO
19
Validate data in form
Data validation is the process of ensuring that computer input is clean,
correct, and useful.
Typical validation tasks are:
has the user filled in all required fields?
has the user entered a valid date?
has the user entered text in a numeric field?
Data validation is to ensure correct input to a computer application.
Deployed in many different ways.
Server side validation is performed by a web server, after input has
been sent to the server.
Client side validation is performed by a web browser, before input is
sent to a web server.
Example: check form field is empty
22
ATTRIBUTE NAME
23
DEMO USES ATTRIBUTE NAME TO ACCESS A
GROUP OF ELEMENTS
<html>
<body >
<input type="checkbox" name="test" value="how"/>
<input type="checkbox" name="test" value="are"/>
<input type="checkbox" name="test" value="you"/>
<input type="checkbox" name="test" value="?"/>
<script type="text/javascript">
var ckAr = document.getElementsByName("test");
var str = "";
for (var i = 0; i < ckAr.length; i++) {
str = str + " "+ ckAr[i].value
}
alert(str);
</script>
</body>
</html>
24
SELECT BOX
Select Box
<select id="country">
<option selected="selected">Viet Nam</option>
<option value="Anh">Anh</option>
<option value="My">My</option>
</select>>
document.getElementById("country").value
<html ><head>
<script type="text/javascript">
function chonQuocGiaClick(){
var name = document.getElementById("country").value;
alert(name);
}
</script>
</head>
<body>
<select id="country">
<option selected="selected">Viet Nam</option>
<option value="Anh">Anh</option>
<option value="My">My</option>
</select>
<input type ="button" value="Chọn Quốc Gia"
onclick="chonQuocGiaClick()" />
</body></html>
26
DEMO SETTING VALUE FOR SELECT
BOX
<body>
<p><input type="button" value="Chau A" onclick= "chauAClick();" />
<input type="button" value="Chau Au" onclick="chauAuClick();"/>
<input type="button" value="Chau My" onclick="chauMy();" /> </p>
<p><select id="country">
<option selected="selected">Viet Nam</option>
<option value="Anh">Anh</option>
<option value="My">My</option>
</select></p>
</body>
Click vào
Chau Au
Click vào
Chau My
27
DEMO SETTING VALUE FOR SELECT
BOX
<script type="text/javascript">
function chauAClick() {
document.getElementById("country").value = "Viet Nam";
}
function chauAuClick() {
document.getElementById("country").value = "Anh";
}
function chauMy() {
document.getElementById("country").value = "My";
}
</script>
<body>
<p><input type="button" value="Chau A" onclick= "chauAClick();" />
<input type="button" value="Chau Au" onclick="chauAuClick();"/>
<input type="button" value="Chau My" onclick="chauMy();" /> </p>
<p><select id="country">
<option selected="selected">Viet Nam</option>
<option value="Anh">Anh</option>
<option value="My">My</option>
</select></p> 28
CHECKBOX
29
DEMO CHECKBOX
<html >
<body>
Cac loai kem trong cua hang:<br />
<input type="checkbox" value="Kem que" name="ckKem"/>Kem que <br />
<input type="checkbox" value="Kem dau" name="ckKem"/>Kem dau <br />
<input type="checkbox" value="Kem bo" name="ckKem"/>Kem bo <br />
<input type="checkbox" value="Kem vani" name="ckKem"/>Kem vani <br />
<input type="button" onclick="chonKem();" value="Chon kem" />
<p id="pKemDaChon"></p>
</body></html>
30
DEMO CHECKBOX
<html ><head>
<script type="text/javascript">
function chonKem() {
var str = "Nhung loai kem ban da chon la:<br/>";
var kemAr = document.getElementsByName("ckKem");
for (var i = 0; i < kemAr.length; i++) {
if (kemAr[i].checked) {
str= str + kemAr[i].value +"<br/>"
}
}
document.getElementById("pKemDaChon").innerHTML = str;
}
</script>
</head><body>
Cac loai kem trong cua hang:<br />
<input type="checkbox" value="Kem que" name="ckKem"/>Kem que <br />
<input type="checkbox" value="Kem dau" name="ckKem"/>Kem dau <br />
<input type="checkbox" value="Kem bo" name="ckKem"/>Kem bo <br />
<input type="checkbox" value="Kem vani" name="ckKem"/>Kem vani <br />
<input type="button" onclick="chonKem();" value="Chon kem" />
<p id="pKemDaChon"></p></body></html>
31
RADIO BUTTON
33
DEMO RADIO BUTTON
var size="";
var sizeAr = document.getElementsByName("rdSize");
for (var i = 0; i < sizeAr.length; i++) {
if (sizeAr[i].checked) {
size = sizeAr[i].value;
}
}
str = str + "Kich thuoc ban da chon la: " + size;
document.getElementById("pKichThuocDaChon").innerHTML = size;
34
How to use regular expressions
How to create and use regular expression
• A regular expression defines a pattern that can be
searched for in a string. This pattern is stored in a
regular expression object.
• To create a regular expression object, you can use
the RegExp constructor function or use regular
expression literal.
How to create and use regular expression
(cont.)
How to create and use regular expression
(cont.)
How to create regular expression patterns
How to create regular expression patterns
(cont.)
How to create regular expression patterns
(cont.)
How to create regular expression patterns
(cont.)
How to create regular expression patterns
(cont.)
Regular expressions for data validation
Regular expressions for data validation
(cont.)
How to handle exceptions
Concept of Exception
• An exception is an abnormal condition that arise
out of an extraordinary situation disrupting the
flow of program instruction.
• In programs, exceptions can
occur due to any of the following
reasons:
– Programming errors
– Client code errors
– Errors beyond the control of a
program
Handle checked exception