[英]Bootstrap-vue textarea.selectionStart is NaN or undefined
[英]why doesn't textarea.selectionStart work on ie?
这已经到了绝对沮丧的地步。 我们正在尝试编写一些js,以便在与textarea相关联的文本中的特定位置插入文本。 看起来应该很简单,但到目前为止我们还没有弄清楚。
使我们的过程复杂化的是,我们有一个浮动菜单,您可以从中选择要插入的内容。 所以顺序是:
位置到文本区域中所需的插入位置。 使用调试器断点,Visual Studio调试器指示textarea对象的selectionStart和selectionEnd定义为具有正确值的数字。 如果在那个休息时间,您尝试编写访问selectionStart和selectionEnd的js,则会得到未定义的消息(这没有意义,但是这里有很多帖子说其他人也有同样的问题)
现在我们将其放置在浮动菜单上,该菜单不过是表上的div包装器,其中td元素调用此函数来插入文本)。 如果我们恰好单击td元素,则文本将插入到其所属位置。 如果随时在td元素外部但在菜单内部单击,则文档选择将重置为文本区域的开头,并且在错误的位置插入。
我们已经尝试捕获与菜单相关联的onclick事件链,并且没有明显的事情在进行,这会导致文档选择发生变化,但它的确如此。
我们尝试了多种变通办法,将onClick放在文本区域本身上,并记住文本区域的当前范围,但该范围始终位于缓冲区的最前面。
所以简单地说:为什么createTextRange和文档选择在ie上不表现合理,解决方法是什么?
jquery可以解决此问题的方法是什么,因为对此发布的最常见答案似乎是使用jquery。
这是一种将在插入符号处插入的方法。
function insertAtCaret ( areaId, text ) {
var txtarea = document.getElementById(areaId),
scrollPos = txtarea.scrollTop,
strPos = 0,
range, front, back,
br = ((txtarea.selectionStart || txtarea.selectionStart == '0') ?
"ff" : (document.selection ? "ie" : false ) );
if (br == "ie") {
txtarea.focus();
range = document.selection.createRange();
range.moveStart ('character', -txtarea.value.length);
strPos = range.text.length;
}
else if (br == "ff") strPos = txtarea.selectionStart;
front = (txtarea.value).substring(0,strPos);
back = (txtarea.value).substring(strPos,txtarea.value.length);
txtarea.value=front+text+back;
strPos = strPos + text.length;
if (br == "ie") {
txtarea.focus();
range = document.selection.createRange();
range.moveStart ('character', -txtarea.value.length);
range.moveStart ('character', strPos);
range.moveEnd ('character', 0);
range.select();
}
else if (br == "ff") {
txtarea.selectionStart = strPos;
txtarea.selectionEnd = strPos;
txtarea.focus();
}
txtarea.scrollTop = scrollPos;
};
我最初是从SO的答案中得到的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.