Mit*_*ran 3 javascript jquery textarea line-breaks word-wrap
如何判断文本区域中的长文本是否换行为两行或多行?
我不是在谈论这里讨论的新行字符。
我知道插件Textarea Line Count
有没有更简单的方法?
我对此进行了实验,并提出了一个涉及以下内容的黑客:
禁用文本区域中的文本换行(加上禁用填充)
检查 textarea 是否scrollWidth大于width。
我使用的基本逻辑是,如果文本跨越多行,这意味着当关闭换行时,我们应该得到一个水平滚动条。
演示: http: //jsfiddle.net/fnG3d/
免责声明:下面的代码是 10 分钟的修改结果,绝对不是生产质量
function checkMulti(id) {
var ta = $('#'+id), multi = false;
// disable wrapping, padding and enable horiz scoll
ta.addClass('nowrap');
// check if there is anything to be scrolled horizontally (means multi-lines otherwise)
multi = ( ta[0].scrollWidth > ta.width() );
// checking done. remove the class.
ta.removeClass('nowrap');
alert('Spread over multiple-lines: ' + multi);
}
/* the nowrap class */
.nowrap {
overflow-x: scroll;
white-space: nowrap;
padding: 0;
}
Run Code Online (Sandbox Code Playgroud)