在 jQuery 中,:input 與 input 看似相似,但選取範圍不同。
input 是 HTML 標籤選擇器,只會選取 <input> 元素;:input 則是 jQuery 擴充選擇器,會選取所有表單控制元件,包含 input、textarea、select、button 等。
$(function () {
$(':input').focus(function () {
$(this).addClass('focus');
}).blur(function () {
$(this).removeClass('focus');
});
});
在這個例子中,:input 會讓文字框、密碼框與多行文字區域都套用相同效果。若改成 input,則只會影響 <input> 元素。