CHƯƠNG XVII(tt)
MÔ HÌNH ĐỐI TƯỢNG
I. Đối tượng form
Form là một thành phần dùng để thu thập dữ liệu,
thông tin từ người dùng. Mỗi phần tử trong form là
một đối tượng trong DOM. Do đó mỗi phần tử trên
form cũng có những sự kiện và phương thức của nó
a) Các sự kiện của các phần tử trên form .
Phần tử Tên sự kiện
Button onClick
Checkbox onClick
Form OnSubmit, onReset
Textbox OnBlur,OnChange,OnFocus,Onselect
Radio OnClick
Reset button OnClick
Dropdown menu OnBlur,onChange,onFocus,onSelect
Submit button OnClick
Textarea OnBlur,OnChange,OnFocus,Onselect
T/tính Mô tả
Ví dụ
Action Trả về đường dẫn
(URL) đến tập tin
xử lý của form thứ i
Document.forms[i].action
Length Trả về số form trên
trang web
Hoặc trả về số phần
tử trên form thứ i
Countform=document.forms.length
Countfield=document.forms[i].length
Name Trả về giá trị tên
của form thứ i
Nameform=document.forms[i].name
Method Các định phương
thức của form thứ i
Methodform=document.forms[i].method
elements mảng element chứa
các phần tử trên
form
document.forms[i].elements[j].value
2. Thuộc tính của form
Ví dụ:
<script>
function kq()
{
sptform=document.form1.length
document.form1.spt.value=sptform
tenform=document.forms[0].name
document.form1.formname.value=tenform
}
</script>
3. Các thuộc tính trên mảng element
T/ tính Mô tả
Ví dụ
Name
Xác định tên của
phần tử j trên
form thứ i.
document.forms[i].elements[j].name
Type
Xác định lọai của
đối tượng
document.forms[i].elements[j].type
Value
Xác định giá trị của
phần tử thứ j trong
form i.
document.forms[i].elements[j].value
Checked
Xác định phần tử
thứ j có được
checked không
document.form[i].
elements[j].checked
trả về giá trị true hoặc false
Disabled
Thiết lập chế độ
vô hiệu hóa đối
tượng
document.form[i].elements[j].
disabled
isDisable
Kiểm tra phần tử
có bị vô hiệu hóa
hay không
document.form[i].elements[j].
isDisable
readOnly
Cho phép/không
thay đổi nội
dung của phần tử
document.forms[i].elements[j]
.readOnly
Ví dụ:
function loadform()
{
document.form1.ok.disabled=true;
}
function validateform()
{
a=document.form1.user.value;
if(a!="")
document.form1.ok.disabled=false;
else
document.form1.ok.disabled=true;
}
4) Phương thức trên mảng element
•
Focus (): Đưa con trỏ về lại text box hoặc
dropdownmenu
document.forms[i].elements[j].focus()
Ví dụ:
document.form1.user.focus();
Lưu ý:
Nếu ta đang làm việc trên document hiện hành, thì
có thể bỏ qua document
nameForm.nameField.property
hoặc
nameForm.nameField.method
II. Các phần tử trên form
1. Thao tác trên trường radio
Để lấy giá trị của trường radio ta phải sử dụng đến
mảng element. Duyệt qua tất cả các phần tử và
kiểm tra phần tử đó có được checked không ?
Cú pháp:
Countfield=nameform.length
for(var i=0 ; i<Countfield ; i++)
r
Giatri=nameform.elements[i].value
Ví dụ:
<script>
function chontrinhduyet()
{
count=document.form1.length;
for(i=0; i<count; i++)
{
if(document.form1.elements[i].type=="radio"&&
document.form1.elements[i].checked==true)
gt=document.form1.elements[i].value
}
document.form1.chon.value=gt
}
</script>
Ví dụ : Checkbox
<script type="text/javascript">
function check()
{
coffee=document.forms[0].coffee
answer=document.forms[0].answer
txt=""
for (i = 0; i<coffee.length; ++ i)
{
if (coffee[i].checked)
{txt=txt + coffee[i].value + " "}
}
answer.value="You ordered a coffee with " + txt
}
</script>
2. Thao tác trên dropdownmenu
a) Thuộc tính và phương thức của dropdownmenu
−
Thuôc tính
Thuộc tính Mô tả
Ví dụ
length
Trả về số phần tử
trong danh sách
dropdownmenu.
spt=nameform.namefield.length
selectedIndex
trả về chỉ số của
phần tử được chọn
trong danh sách
spt=nameform.namefield.
selectedIndex
options
mảng option được
đánh chỉ số từ
0->spt-1
Ví dụ:
<script type="text/javascript">
function chonkhoa()
{
option=document.forms[0].khoa.options[document.
forms[0].khoa.selectedIndex].text
txt=document.forms[0].chon.value
txt=txt + option
document.forms[0].chon.value=txt
}
</script>
III. THAY ĐỔI NỘI DUNG ĐỘNG TRÊN TRANG
1. Thay đổi nội dung trên trang dựa vào inner và outer
Ta dùng đặc tính inner và outer để thay đổi nội dung
hoặc lấy giá trị của một vùng nào đó trên trang web.
a) Phân biệt giữa inner và outer
Inner là nội dung chứa bên trong của đối tượng
chứa ID. Inner gồm có
−
InnerHTML lấy nội dung text và tag HTML
bên trong đối tượng ID
−
innerText: chỉ lấy nội dung text bên trong dối
tượng ID
Outer là phần inner và bản thân đối tượng chứa
ID. Outer gồm có
−
outerHTML lấy nội dung text và tag HTML
của cả đối tượng ID
−
outerText : lấy nội dung text
Ví dụ:
<Div ID=Intro>Monitor<B> SAMSUNG</B></Div>
inner
outer
Ví dụ:
Var s1,s2
s1=Intro.outerText
s2=Intro.innerText
thì s1 và s2 đều nhận giá trị Monitor SAMSUNG
Ví dụ
s1=Intro.outerHTML
s2=Intro.innerHTML
Thì s1=<Div Id=Intro>Monitor<B>
SAMSUNG</B></Div>
Và s2=Monitor<B> SAMSUNG</B>
Ví dụ:thiết kế form có dạng:
<script>
function chonsp()
{
prod=document.form1.masp.value;
hinhsp=document.forms[0].hinh.options[document.
forms[0].hinh.selectedIndex].text;
price=document.form1.giasp.value;
ma.innerText=prod;
hinh.innerText=hinhsp;
gia.innerText=price
}
function ChangeImage(path)
{
h.innerHTML="<img src= /images/" + path +"
width=100 height=100 >"
}
Gọi hàm ChangeImage(value)
<select name="hinh" id="hinh"
onchange="ChangeImage(value)">
<option value=" /images/h1.gif">hình 1</option>
<option value=" /images/h4.gif">hình 2</option>
<option value=" /images/h3.gif">hình 3</option>
</select>
CHƯƠNG XVIII
FILTER VÀ TRASITION
I. FILTER
1. Khái niệm:
Filter là hiệu ứng đặc biệt được áp dụng cho một
đối tượng trong trang web để thay đổi sự thể hiện
của nó. Hiệu ứng này chỉ được Internet Explorer
hỗ trợ.
2. Cách tạo:
−
Dùng CSS
−
Dùng Javascript
a) Sử dụng CSS:
Cú pháp:
Filter: filter_Name(list parameters)
Một số filter thôngdụng
−
Alpha(Opacity : 0-100, Style : 0,1,2,3 ):Bộ lọc
trong suốt
•
opacity từ 0 (trong suốt) tới 100 (bình
thường).
•
Style là kiểu lọc
−
Blur(Direction:0-360, Strength:1-100, Add :0/1)
(cộng hình gốc)Bộ lọc nhoè
•
Direction: chiều làm nhoè tính theo góc,
•
strength chỉ độ nhoè mạnh hay yếu
Ví dụ:
<img border = "0" src = "van.gif" width = "110"
height= "120" style= "filter: alpha (opacity = 90, style
= 2)">
<img border="0" src="van.gif" width="110"
height="120" style=" filter: blur (strength = 10) " >