我在asp.net表單上有一個多行文本框,並且我正在調用過去事件的以下javascript函數。換行符在javascript中被忽略多行文本框
如果我粘貼任何數據,則會忽略新行。
我不知道它是否與JavaScript功能有關。我可以在內部文本中看到'/ n',但不知何故它沒有被正確渲染。 「你好\ nworld」正在呈現「你好世界」
function handlepaste(textbox, e, label, len) {
var savedcontent = textbox.innerHTML;
if (e && e.clipboardData && e.clipboardData.getData) {// Webkit - get data from clipboard, put into editdiv, cleanup, then cancel event
if (/text\/html/.test(e.clipboardData.types)) {
textbox.innerHTML = e.clipboardData.getData('text/html');
}
else if (/text\/plain/.test(e.clipboardData.types)) {
textbox.innerHTML = e.clipboardData.getData('text/plain');
}
else {
textbox.innerHTML = "";
}
waitforpastedata(textbox, savedcontent);
if (e.preventDefault) {
e.stopPropagation();
e.preventDefault();
}
return false;
}
else {// Everything else - empty editdiv and allow browser to paste content into it, then cleanup
textbox.innerHTML = "";
waitforpastedata(textbox, savedcontent, label, len);
return true;
}
}
function waitforpastedata(elem, savedcontent, label, len) {
if (elem.childNodes && elem.childNodes.length > 0) {
processpaste(elem, savedcontent, label, len);
}
else {
that = {
e: elem,
s: savedcontent
}
that.callself = function() {
waitforpastedata(that.e, that.s, label, len)
}
setTimeout(that.callself, 20);
}
}
function processpaste(textbox, savedcontent, label, len) {
pasteddata = textbox.innerHTML;
if (savedcontent.length > 0) {
textbox.innerHTML = savedcontent + pasteddata ;
}
if (pasteddata.length > len) {
textbox.value = textbox.value.substring(0, len);
document.getElementById(label).className = "red-text";
document.getElementById(label).innerHTML = 'You are trying to type too much information ';
}
else {
document.getElementById(label).className = "grey-text";
document.getElementById(label).innerHTML = textbox.value.length.toString() + ' of ' + len + ' chars';
}
}
我試過textbox.innerHTML = savedcontent.replace(/ \ n/g,'
')+ pasteddata.replace(/ \ n/g,'
');但兩個新行'
'被渲染在新行'
' – user1263981 2014-11-14 16:26:46