- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个带有属性 wrap="hard"的文本区域,这意味着我应该能够在提交到服务器后获得换行符(新行)——这可行。但我想做的是获取在提交之前创建的新行。
为什么?因为我想计算当前可见的行数。现在我不是在谈论回车行。我说的是通过限制文本区域的宽度(或设置 cols 属性)创建的行。
我有这个文本框:
<textarea id="myarea" name="myarea" cols="35" rows="10" wrap="hard">
Some example text here. Hello my name is something I should be able to blabla.
</textarea>
在浏览器中输出:
这里有一些示例文本。你好,我的名字是
我应该能够 blabla 的东西。
行数 = 2
我试过了:
$('#myarea').html().split("\n").length
$('#myarea').val().split("
").length
$('#myarea').val().split("\r").length
还有一些组合...
但没有一个有效。如果不编写计算每个字符然后创建换行符的脚本,我什至可能会问什么?我希望这会“自动”发生......
如果这不可能,为什么服务器可以解释(找到)新行,而我不能?
谢谢!
最佳答案
似乎没有内置工具来获取此值。但是我们可以计算一下。假设 textarea 中的 line-height 是一个已知值(你可以在 css 中显式定义它)。所以计算行数的代码是这样的:
var area = $('#myarea').get(0);
//save the initial height of textarea
var initialHeight = area.style.height;
//set the height to 0 so scrollHeight will always return dynamic height
area.style.height = '0px';
//here we assume that the line-height equals to 15
var numberOfRows = Math.floor(area.scrollHeight / 15);
//restore textarea height
area.style.height = initialHeight;
console.log(numberOfRows);
工作示例 http://jsfiddle.net/J5UpF/
更新
刚刚用我的代码找出了 Chrome 中的错误。我没有考虑出现在 textarea 右侧的滚动条。这有时会导致错误的行数计算。然而,它在 Firefox 中就像一个魅力。要解决此问题,我们必须计算滚动条宽度的差异并相应地调整文本区域的宽度:
var area = $('#myarea').get(0);
var $area = $('#myarea');
//save the initial height of textarea
var initialHeight = area.style.height;
var scrollbarWidthInitial = area.offsetWidth - area.clientWidth;
//set the height to 0 so scrollHeight will always return dynamic height
area.style.height = '0px';
var scrollbarWidth = area.offsetWidth - area.clientWidth;
var scrollbarWidthDiff = scrollbarWidth - scrollbarWidthInitial;
$area.width($area.width() + scrollbarWidthDiff);
//here we assume that the line-height equals to 15
var numberOfRows = Math.floor(area.scrollHeight / 15);
//restore textarea height
area.style.height = initialHeight;
$area.width($area.width() - scrollbarWidthDiff);
console.log(numberOfRows);
更新示例 http://jsfiddle.net/J5UpF/3/
更新 2
在 Chrome 中发现了一个新的令人敬畏的错误!当使用 placeholder
属性时,Chrome 会计算占位符文本的行数。解决方法很简单:当您在 textarea 中输入一些数据时,只需备份占位符属性,并在数据被清除时恢复它。
关于javascript - 在提交表单之前阅读文本区域中的换行符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16759756/
我有一个带有 的表格我想在服务器端捕获该文本区域中的任何换行符,并将它们替换为 . 这可能吗? 我尝试设置 white-space:pre在 textarea 上的 CSS,但仍然不够。 最佳答案
我正在通过 FileStream 更改文件(这是一个非常大的文件,我只需要更改标题而不重写整个文件。 该文件可以有 Unix 或 Windows 换行符,知道哪一个对我来说很重要,这样我可以在更新文件
如何以编程方式(不使用 vi)将 DOS/Windows 换行符转换为 Unix 换行符? dos2unix 和 unix2dos 命令在某些系统上不可用。 如何使用 sed、awk 和 tr 等命令
我编辑了一个 BibteX 文件,到目前为止,它显示了我需要的所有信息。 FUNCTION {electronic} { output.bibitem format.btitle "title"
您的软件是否处理来自其他系统的换行符? Linux/BSD linefeed ^J 10 x0A Windows/IBM return linefeed ^M^
我发现 Windows 命令行重定向会自动将 '\n' 替换为 '\r\n'。有什么方法可以避免这种情况?因为在 stdout 或 stderr 重定向之后,如果您将 '\r\n' 写入控制台,您将得
来自 this question ,这个…… lines = foo.value.split(/\r\n|\r|\n/); 是拆分字符串的一种方法,但如何用换行符将其连接回去? 另外,我想知道如果我说
我正在尝试获取新行,但如果我使用 \n 它不起作用。 任何通过向字符串添加一些东西来换行的方法,例如 \r\n (这也不起作用) gfx.DrawString("Project No \n" + te
我有一串数据,中间有换行符。例如: "Product Name \n Product Color \n Product Quantity \n Product Location \n Product
我正在尝试让 scmCommentPrefix 按照 http://maven.apache.org/plugins/maven-release-plugin/faq.html#scmCommentP
如何检查正则表达式 /^\n/在字符串中 blahblahblah 我似乎无法从 php 的 preg_match 获得返回值 1 . 编辑: 由于某种原因,我的坏处是 CR 本身就是我的换行符。
我很难在与文本字符串相同的行上输出变量。当我使用Write-Host而不是Write-Output时,它可以工作。我想使用Write-Output,因为这似乎是最佳做法(将内容保留在管道中),但是Wr
我正在Powershell中工作,以为here字符串中的特定单词着色。除包含回车/换行符的单词外,其他功能均有效。没有这些字符,如何计算单词的长度? 以下是我正在使用的功能和测试数据。我希望第二行上的
我有一个在Powershell中运行的脚本,并且我希望能够在脚本名称和脚本内容本身之间的结果文本文件输出中添加一行。 当前,从下面开始,行$str_msg = $file,[System.IO.Fil
有人知道smalltalk中字符串的换行符是什么吗? 我试图将字符串拆分为单独的行,但我无法弄清楚smalltalk 中的换行符是什么。 即。 string := 'smalltalk is
我有以下 printf 语句: printf ("%s (%s)\n",$row["word"], $row["definition"]); 我正在尝试解决换行符而不是: word defin
这个问题已经有答案了: how to use dotall flag for regex.exec() (4 个回答) 已关闭 7 年前。 字符串内容
我想用 CSS 换行。我正在使用内容。 td:before { content: "Test\A Test2"; } 它不工作。如何正确
考虑一个具有 UNIX 行结尾的 C++ 文件(即 '\x0a' 而不是 "\x0d\x0a")并包含以下原始字符串文字: const char foo[] = R"(hello^M )"; (其中
我有以下 printf 语句: printf ("%s (%s)\n",$row["word"], $row["definition"]); 我正在尝试解决换行符而不是: word defin
我是一名优秀的程序员,十分优秀!