javascript - 选择开始忽略 Chrome 中文本区域中的新行/换行符

标签 javascript jquery google-chrome selection-api

我正在尝试将文本插入光标位置的文本区域或替换选定的文本片段。该代码的工作原理是插入文本,如果选择了文本,则将替换它。

问题是,如果用户先插入几个换行符,然后尝试插入文本。然后,不是在光标处插入文本,而是将其插入到第二行。例如,如果用户插入换行符并在第 5 行结束,然后单击输入来插入文本,则文本将插入到第 2 行。

var holdFocus;
function updateFocus(x) {
  holdFocus = x;
}

触发者:

<textarea onFocus="updateFocus(this)" cols="53" rows="10" name="entry" id="report" style="width: 98%;"></textarea>

然后插入文本:

function InsertCodeInTextArea(text){
    var tav    = $(holdFocus).val(),
    strPos = $(holdFocus)[0].selectionStart;
    var endPos = $(holdFocus)[0].selectionEnd;
    front  = (tav).substring(0,strPos),
    back   = (tav).substring(endPos,tav.length);
    var textValue = text.split("%lb%").join("\r\n");
    $(holdFocus).val(front+ textValue + back);
}

通过点击触发:

<input class="input" type="button" name="LODCTRRAPPA" value ="LODCTRRAPPA" onClick="InsertCodeInTextArea('Location: %lb%Onset: %lb%Duration: %lb%Course: %lb%Type of pain/symptom: %lb%')"/>

此问题似乎仅发生在 Chrome 中。 Safari 和 FF 都可以。未测试 IE。我确信这也是几周前才发生的。

最佳答案

使用普通 JavaScript

来自jQuery docs on val() :

Note: At present, using .val() on elements strips carriage return characters from the browser-reported value. When this value is sent to the server via XHR, however, carriage returns are preserved (or added by browsers which do not include them in the raw value). A workaround for this issue can be achieved using a valHook as follows:
$.valHooks.textarea = { get: function( elem ) { return elem.value.replace( /\r?\n/g, "\r\n" ); } };

像下面这样的东西就足够了:

var holdFocus;
function updateFocus(x) {
    holdFocus = x;
}

function InsertCodeInTextArea(text){
    var tav = holdFocus.value;
    holdFocus.value = tav.substring(0, holdFocus.selectionStart) +
                      text.split("%lb%").join("\r\n") +
                      tav.substring(holdFocus.selectionEnd, tav.length);
}
<textarea autofocus onFocus="updateFocus(this)" cols="53" rows="10" name="entry" id="report" style="width: 98%;">foo

bar

baz</textarea>
<input class="input" type="button" name="LODCTRRAPPA" value ="LODCTRRAPPA" onClick="InsertCodeInTextArea('Location: %lb%Onset: %lb%Duration: %lb%Course: %lb%Type of pain/symptom: %lb%')"/>

更新

这是一个错误!请参阅productforums.google.com

... if you click out of the textarea and then click back in, and then hit the button the error is now gone and it works.

测试了我的代码片段,在单击退出(失去焦点)然后单击返回(重新获得焦点)后,它确实起作用了。

这显然会导致所做的任何选择丢失,并且要求用户这样做是不切实际的。

如果我找到编程解决方法,我会更新此答案。

关于javascript - 选择开始忽略 Chrome 中文本区域中的新行/换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44731983/

相关文章:

javascript - 查找 tr 中的复选框是否被选中

JavaScript 正则表达式使用 n 个任意字符格式化手机以进行扩展

javascript - 我如何使用这个查找数组并在 javascript 中匹配数组?

javascript - Jquery Javascript - 使内联 js 成为一个函数

google-chrome - 如何在 Chrome 中调试 HTTP POST?

google-chrome - 我的 chrome 扩展程序弹出窗口会在几秒钟后打开,与其他扩展程序相比,它的速度很慢

html - 在 Google Chrome 中打印表格时,内容与标题重叠

javascript - Discord.js - 如何为数组字符串设置值?

javascript - JSONSerializer 呈现 Java 字符串数组

jquery ajax 按钮 onclick 事件不起作用