当我在非可调整大小的文本区域像hello world, this is a demo
类型和文本区域是足够小,它看起来就像这样:检测自然换行符的JavaScript
hello world,
this is a demo
这不是由一个\ n引起的或者其他的东西。 如何检测文本区域中的自然换行符? 小提琴可以在这里找到:http://jsfiddle.net/yx6B7/ 正如你所看到的,有一个换行符,但JavaScript只是说它是一个没有任何换行符的大行。
当我在非可调整大小的文本区域像hello world, this is a demo
类型和文本区域是足够小,它看起来就像这样:检测自然换行符的JavaScript
hello world,
this is a demo
这不是由一个\ n引起的或者其他的东西。 如何检测文本区域中的自然换行符? 小提琴可以在这里找到:http://jsfiddle.net/yx6B7/ 正如你所看到的,有一个换行符,但JavaScript只是说它是一个没有任何换行符的大行。
最后,我发现在互联网上这个脚本:
function ApplyLineBreaks(strTextAreaId) {
var oTextarea = document.getElementById(strTextAreaId);
if (oTextarea.wrap) {
oTextarea.setAttribute("wrap", "off");
}
else {
oTextarea.setAttribute("wrap", "off");
var newArea = oTextarea.cloneNode(true);
newArea.value = oTextarea.value;
oTextarea.parentNode.replaceChild(newArea, oTextarea);
oTextarea = newArea;
}
var strRawValue = oTextarea.value;
oTextarea.value = "";
var nEmptyWidth = oTextarea.scrollWidth;
var nLastWrappingIndex = -1;
for (var i = 0; i < strRawValue.length; i++) {
var curChar = strRawValue.charAt(i);
if (curChar == ' ' || curChar == '-' || curChar == '+')
nLastWrappingIndex = i;
oTextarea.value += curChar;
if (oTextarea.scrollWidth > nEmptyWidth) {
var buffer = "";
if (nLastWrappingIndex >= 0) {
for (var j = nLastWrappingIndex + 1; j < i; j++)
buffer += strRawValue.charAt(j);
nLastWrappingIndex = -1;
}
buffer += curChar;
oTextarea.value = oTextarea.value.substr(0, oTextarea.value.length - buffer.length);
oTextarea.value += "\n" + buffer;
}
}
oTextarea.setAttribute("wrap", "");
document.getElementById("pnlPreview").innerHTML = oTextarea.value.replace(new RegExp("\\n", "g"), "<br />");
}
这是工作的罚款。
这不是一个javascript问题。
看看word-wrap
,white-space
和overflow
CSS属性。
相关问题:http://stackoverflow.com/questions/572298/css-how-to-stop-text-from-ta-king-up-more-than-1-line –
[在textarea中查找“换行符”,这是包装ARABIC文字的可能的重复](http://stackoverflow.com/questions/4719777/finding-line-breaks-in-textarea-that-is-字包装阿拉伯文) –
检测,然后...?什么是最终目标? – georg
@ thg435:将这些换行符转换为真正的换行符。 – SheperdOfFire