[英]How add \n to enter key press
當用戶按下 Enter 鍵時,我需要將 '\\n' 添加到我的文本區域標簽中。 因為默認輸入只添加視覺新字符串,如果我從其他地方的文本區域復制文本,我有一個字符串。
如果我嘗試
$('.body__textarea').on('keydown', function (e) {
if ( e.keyCode === 13 ) {
e.preventDefault();
const oldValue = $(this).val();
const newValue = oldValue + '\n'
$(this).val(newValue);
}
})
新行添加在字符串的末尾,但需要插入符號的位置。
你應該使用<br>
而不是\\n
我的解決方案是更改“復制”事件:
const source = document.querySelector('.body__textarea');
source.addEventListener('copy', (event) => {
const selection = document.getSelection();
event.clipboardData.setData('text/plain', selection.toString());
event.preventDefault();
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.