Clearing Textarea on Blur (jQuery)
// Clearing Textarea
$('textarea').focus(function() {
var $this = $(this);
$.data(this, 'img', $this.css('background-image'));
$this.css('background-image', 'none');
});
$('textarea').blur(function() {
if($.trim($('textarea').val()).length){
$this.css('background-image', 'none');
} else {
$(this).css('background-image', $.data(this, 'img'));
}
});
When I click out of the textarea, and although there is content present in it, I still see the background image.
Thanks开发者_如何转开发 for your help!
In your blur function, you have $this, but it is never defined. You only defined it in the scope of the focus() function.
Adding to what Matt said. $this
wasn't defined. What you needed to do was $(this)
:
$('textarea').blur(function() {
if($.trim($('textarea').val()).length){
// Added () around $this
$(this).css('background-image', 'none');
} else {
$(this).css('background-image', $.data(this, 'img'));
}
});
精彩评论