在 JavaScript 中,将文本区域中的 '\r\n' 替换为 '< br>'?
在 JavaScript 中,将文本区域中的 '\r\n' 替换为 '< br>'?
这个问题已经有答案了:
元素替换字符串中的所有换行符?
当我按Enter键时,我想在文本区域中插入\"
\"而不是换行符\'\\n\\r\'
,例如:
我希望文本区域中的文本是:
"hello dear",
而不是
"hello dear"
我尝试过这段代码,但没有成功:
$('#inputText').bind('keyup', function(e) { var data = $('#inputText').val(); $('#inputText').text(data.replace(/\n/g, " ")); }
admin 更改状态以发布 2023年5月23日
如果你的代码满足以下条件,它将能正常工作:
1.将bind()
替换为on()
(因为bind()
已过时)
2.text()
需要替换为.val()
这是可工作的示例(也请检查注释):
// applied mouseout to prevent repetition on each key-press //you can apply keyup also no problem $('#inputText').on('mouseout', function(e) { var data = $('#inputText').val(); $('#inputText').val(data.replace(/\n/g, "")); // text() need be val() }); // here ); missing in your given code