在textarea的占位符属性中插入换行符? [英] Insert line break inside placeholder attribute of a textarea?
问题描述
我尝试了一些方法,但都没有成功。有人知道一个巧妙的方法来解决这个问题吗?
< textarea placeholder ='这是一行\\\
this应该是新行>< / textarea>
更新:它在chrome中不起作用。这只是textarea的宽度。
请参阅: http://jsfiddle.net/pdXRx /
您可以做的是将文本添加为值
,它符合换行符 \\\
。
$('textarea')。attr('value','这是一行\这应该是一个新行');
然后你可以在 focus
和在 blur
上应用它(如果为空)。像这样的东西
var placeholder ='这是一行\这应该是一个新行';
$('textarea')。attr('value',placeholder);
$ b $('textarea')。focus(function(){
if($(this).val()===占位符){
$(this)。 attr('value','');
}
}); $($)
$('textarea')。blur(function(){
if($(this).val()===''){
$(this) .attr('value',占位符);
}
});
示例: http://jsfiddle.net/airandfingers/pdXRx/247/ 非纯CSS而不是干净,但是这样做。 I have tried a few approaches but none worked. Does anyone know a the nifty trick to get around this? UPDATE: It doesn't work in chrome. It was just the textarea width. See: http://jsfiddle.net/pdXRx/ What you could do is add the text as Then you could remove it on Example: http://jsfiddle.net/airandfingers/pdXRx/247/ Not pure CSS and not clean but does the trick. 这篇关于在textarea的占位符属性中插入换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
<textarea placeholder='This is a line \n this should be a new line'></textarea>
<textarea placeholder='This is a line
should this be a new line?'></textarea> <!-- this works in chrome apparently -->
value
, which respects the line break \n
.$('textarea').attr('value', 'This is a line \nthis should be a new line');
focus
and apply it back (if empty) on blur
. Something like thisvar 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);
}
});