Cách dùng .blur() trong jQuery
Blur là sự kiện xảy ra khi con trỏ chuột đi ra ngoài đối tượng. Ví dụ bạn có một ô input, khi người dùng nhập dữ liệu xong và nhấn tab để di chuyển con trỏ sang ô input khác thì sẽ xảy ra sự kiện blur.

Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.
Chúng ta có 3 cách sử dụng như sau:
Cú pháp
.blur( handler )
Trong đó handler
là hàm xử lý sự kiện.
.blur( [eventData ], handler )
Trong đó:
Bài viết này được đăng tại [free tuts .net]
handler
: là hàm xử lý sự kiện.-
eventData
là một object sẽ được đưa vào hàmhandler
.
.blur()
Cách này không có tham số, nó dùng để kích hoạt sự kiện.
Lưu ý: Đọc bài xử lý sự kiện và kích hoạt sự kiện để hiểu rõ hai từ khóa trên.
Ví dụ
Mình sẽ làm một ví dụ rất phổ biến như sau: Khi nhập dữ liệu nếu người dùng không nhập mà di chuyển sang chỗ khác thì thông báo họ là bắt buộc nhập.
Code
RUN
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <script> $(document).ready( function () { $( "#username" ).blur( function () { var value = $( this ).val(); if (value === '' ){ alert( "Vui lòng nhập username" ); } }); $( "#password" ).blur( function () { var value = $( this ).val(); if (value === '' ){ alert( "Vui lòng nhập password" ); } }); }); </script> <form> <input id= "username" type= "text" value= "" placeholder= "Username" > <br/><br/> <input id= "password" type= "password" value= "" placeholder= "Password" > <br/><br/> <input type= "text" value= "LOGIN" > </form> |
Cùng chuyên mục:
JQUERY SELECTOR
Cách dùng All Selector (“*”) trong jQuery
Cách dùng :animated Selector trong jQuery
Cách dùng Attribute Contains Prefix Selector [name|=”value”] trong jQuery
Cách dùng Attribute Contains Selector [name*=”value”] trong jQuery
Cách dùng Attribute Contains Word Selector [name~=”value”] trong jQuery
Cách dùng Attribute Ends With Selector [name$=”value”] trong jQuery
Cách dùng Attribute Equals Selector [name=”value”] trong jQuery
Cách dùng Attribute Not Equal Selector [name!=”value”] trong jQuery
Cách dùng Attribute Starts With Selector [name^=”value”] trong jQuery
Cách dùng :button Selector trong jQuery
Cách dùng :checkbox Selector trong jQuery
Cách dùng :checked Selector trong jQuery
Cách dùng Child Selector (“parent > child”) trong jQuery
Cách dùng Class Selector (“.class”) trong jQuery
Cách dùng :contains() Selector trong jQuery
Cách dùng Descendant Selector (“ancestor descendant”) trong jQuery
Cách dùng :disabled Selector trong jQuery
Cách dùng Element Selector (“element”) trong jQuery
Cách dùng :empty Selector trong jQuery
Cách dùng :enabled selector trong jQuery
Cách dùng :eq() Selector trong jQuery
Cách dùng :even Selector trong jQuery
Cách dùng :odd Selector trong jQuery
Cách dùng :file Selector trong jQuery
Cách dùng :first-child Selector trong jQuery
Cách dùng :first-of-type Selector trong jQuery
Cách dùng :first Selector trong jQuery
Cách dùng :focus Selector trong jQuery
Cách dùng :gt() Selector trong jQuery
Cách dùng Has Attribute Selector [name] trong jQuery
Cách dùng :has() Selector trong jQuery
Cách dùng :header Selector trong jQuery
Cách dùng :hidden Selector trong jQuery
Cách dùng ID Selector (“#id”) trong jQuery
Cách dùng :image Selector trong jQuery
Cách dùng :input Selector trong jQuery
Cách dùng :lang() Selector trong jQuery
Cách dùng :last-child Selector trong jQuery
Cách dùng :last-of-type Selector trong jQuery
Cách dùng :last Selector trong jQuery
Cách dùng :lt() Selector trong jQuery
Cách dùng Multiple Attribute Selector [name=”value”][name2=”value2″] trong jQuery
Cách dùng Multiple Selector (“selector1, selector2, selectorN”) trong jQuery
Cách dùng Next Adjacent Selector (“prev + next”) trong jQuery
Cách dùng Next Siblings Selector (“prev ~ siblings”) trong jQuery
Cách dùng :not() Selector trong jQuery
Cách dùng :nth-child() Selector trong jQuery
Cách dùng :nth-last-child() Selector trong jQuery
Cách dùng :nth-of-type() Selector trong jQuery
Cách dùng :nth-last-of-type() Selector trong jQuery
Cách dùng :only-child Selector trong jQuery
Cách dùng :only-of-type Selector trong jQuery
Cách dùng :parent Selector trong jQuery
Cách dùng :password Selector trong jQuery
Cách dùng :radio Selector trong jQuery
Cách dùng :reset Selector trong jQuery
Cách dùng :root Selector trong jQuery
Cách dùng :selected Selector trong jQuery
Cách dùng :submit Selector trong jQuery
Cách dùng :target Selector trong jQuery
Cách dùng :text Selector trong jQuery
Cách dùng :visible Selector trong jQuery
JQUERY EVENTS
Cách dùng .bind() trong JQuery
Cách dùng Click() trong jQuery
Cách dùng dblclick() trong jQuery
.blur() trong jQuery
JQUERY API
CÁC CHỦ ĐỀ
BÀI MỚI NHẤT







