如何判断文本区域中的长文本是否换行为两行或多行?
我不是在谈论所讨论的新行字符here https://stackoverflow.com/questions/3697096/finding-number-of-lines-in-an-html-textarea.
我知道这个插件文本区域行数 https://bitbucket.org/MostThingsWeb/textarea-line-count/wiki/Home
有没有更简单的方法?
我对此进行了实验,并提出了一个涉及以下内容的黑客:
禁用文本区域中的文本换行(加上禁用填充)
检查文本区域是否scrollWidth
大于它的width
.
我使用的基本逻辑是,如果文本跨越多行,这意味着当关闭换行时,我们应该得到一个水平滚动条。
Demo: http://jsfiddle.net/fnG3d/ 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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)