您可以做的是将文本添加为value
,这要注意换行符\n
。
$('textarea').attr('value', 'This is a line \nthis should be a new line');
然后,您可以将其移除,focus
然后将其重新应用(如果为空)blur
。像这样
var placeholder = 'This is a line \nthis should be a new line';
$('textarea').attr('value', placeholder);
$('textarea').focus(function(){
if($(this).val() === placeholder){
$(this).attr('value', '');
}
});
$('textarea').blur(function(){
if($(this).val() ===''){
$(this).attr('value', placeholder);
}
});
不是纯CSS也不 干净, 但是可以解决问题。