getElementByClassName
应该getElementsByClassName
和searchbox_val
应该searchbox_val[0]
。
/* Search Box Leave */
$(".searchbox").blur(function() {
var searchbox_val = document.getElementsByClassName("searchbox");
if (searchbox_val[0].value.length > 0) {
return true;
}
else {
$(this).addClass("blur");
return false;
}
});
Yon也可以尝试如下:
$(".searchbox").blur(function() {
var searchbox = $(this),
searchbox_val = $.trim( searchbox.val() );
if (searchbox_val.length > 0) {
return true;
} else {
searchbox.addClass("blur");
return false;
}
});
[更新]
HTML
<input class="searchbox" type="text" placeholder="Just some text" />
jQuery
$(".searchbox").blur(function() {
var searchbox = this,
searchbox_val = $.trim( this.value );
if (searchbox_val.length > 0) {
$(searchbox).addClass("blur");
return true;
} else {
$(searchbox).removeClass("blur");
return false;
}
});
演示