将插入符号移到内容中输入的右边可编辑,当插入符号在输入的末尾时
Move caret right after input in contenteditable when caret is inside the input at the end of it
我遇到了这个问题。我正在一个内容可编辑的div中插入输入元素。插入符号在最后一个位置的输入中。我想通过执行一些函数来帮助如何将光标移动到输入之后。您将在我的代码中看到,我必须单击(justInserted.click()
)才能进行一些调整大小的操作。如果删除justInserted.focus()
,则插入符号始终位于内容可编辑的开头。我想要一个函数,它可以发现插入符号在内容可编辑的特定输入中,当我调用它时,它会将插入符号放在该特定输入之后。感谢您的帮助:)
我在插入符号处插入的内容如下:
this.insertNodeAtCursor = function(node) {
var sel, range, html;
function containerIsEditable(selection) {
return $(selection.anchorNode).parent().hasClass("editable");
}
if (window.getSelection) {
sel = window.getSelection();
// only if it is a caret otherwise it inserts
// anywhere!
if (containerIsEditable(sel) && sel.getRangeAt
&& sel.rangeCount) {
var previousPosition = sel.getRangeAt(0).startOffset;
sel.getRangeAt(0).insertNode(node);
}
}
else if (document.selection
&& document.selection.createRange) {
range = document.selection.createRange();
html = (node.nodeType == 3) ? node.data
: node.outerHTML;
range.pasteHTML(html);
}
};
添加输入的功能是:
this.addInput = function(suggestEntry, query) {
var id = suggestEntry.id;
var nodeClass = suggestEntry.nodeClass;
var uuid = suggestEntry.uuid;
var clause = null;
if (nodeClass === "Entity"){
clause = new Entity();
clause.uuid = uuid;
clause.id = id;
clause.text = suggestEntry.text;
}
var input = clause.toEditorElementHtml();
this.insertNodeAtCursor(input);
var rand = Math.floor((Math.random() * 1000000) + 1);
input.setAttribute('id', "rand-" + rand);
$rootScope.$broadcast("remove:query",query);
var autoSizingInputs = $('input[autosize="autosize"]');
var justInserted = $('#rand-' + rand);
$compile(autoSizingInputs)($scope);
justInserted.focus();
justInserted.click(); // a bit hacky :/
$(justInserted).val($(justInserted).val() + "");
};
这里有一个片段,它带有一个将插入符号移到焦点输入之后的函数。我已经在Chrome 47.0.2526.111m、Firefox 43.0.4和IE 11中测试过了。
function setCaretAfterFocusedInput(container) {
var input = container.querySelector('input:focus');
if (input) {
container.focus(); // required for firefox
setCaretAfter(input);
}
}
function setCaretAfter(element) {
if (window.getSelection) {
var range = document.createRange();
range.setStartAfter(element);
var selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
}
}
// for demonstration purposes
document.addEventListener('keyup', function(e) {
if (e.which === 16) { // on SHIFT
var container = document.querySelector('div[contenteditable]');
setCaretAfterFocusedInput(container);
}
});
<p>When an input is focused, press shift to move the caret right after the input.</p>
<div contenteditable>
<input type="text" value="input1"><input type="text" value="no whitespace before this">
<br><br>some text<input type="text" value="input3">more text
<br><br>
<input type="text"><p>text in a paragraph, no whitespace before this</p>
<input type="text">
<p>text in a paragraph</p>
</div>
相关文章:
- 删除输入中输入的符号
- 为更改的输入返回插入符号位置的逻辑
- 使用 Javascript 在模糊的输入上显示插入符号
- HTML 符号代码在输入字段中无法正常工作
- 为什么我在自定义验证后无法输入 % 符号
- 将符号等♀附加到输入文本字段
- 正则表达式 - 从文本输入中删除美元符号
- 如何在焦点上垂直设置文本区域/输入中间的默认光标插入符号
- 如何通过 JQuery 输入符号
- 将插入符号移到内容中输入的右边可编辑,当插入符号在输入的末尾时
- 如何使用javascript在输入文本中插入符号
- 刷新文本输入以显示插入符号
- 如何在输入标签中创建货币的静态符号
- 试图用%符号追加输入值
- 如何检查数字输入字段中是否输入了字母字符或符号
- 在AngularJS中使用欧元符号格式化输入
- 确定标记化html输入中的插入符号位置
- 无符号右移函数对负输入不起作用
- 赋值时能否防止输入端插入符号跳转到末尾?
- 禁用输入字段中的符号和非字母