如何从输入字段中获取插入位置?
我通过谷歌发现了一些零碎的东西,但没有任何防弹措施.
基本上像jQuery插件这样的东西是理想的,所以我可以简单地做
$("#myinput").caretPosition()
Run Code Online (Sandbox Code Playgroud) 有没有什么方法可以在jQuery中获取文本输入中的当前光标位置,而无需使用丑陋的浏览器特定代码?
像:
<input id="myTextInput" type="text" value="some text" >
Run Code Online (Sandbox Code Playgroud)
光标在"某些文本"的"x"之后,我可以得到"8"?
TL; DR - 使用非常轻巧和强大的textarea-caret-position 组件库,现在也支持<input ype="text">它.演示http://jsfiddle.net/dandv/aFPA7/
有没有办法知道插入符号在HTML文本字段中的位置?
<input type='text' />
Run Code Online (Sandbox Code Playgroud)
我想根据插入符的位置以像素(并重新定位)div的位置.
注意:我不想知道字符或字符中的位置
<textarea>.我想知道<input>元素中的像素位置.
我想实现像标签编辑器这样的东西.但是,它仅仅意味着那些标签,所以我希望用户看到自动完成建议弹出窗口而不必输入类似@或#的内容,只需要文本本身.
我有一些有用的东西,但弹出窗口显示在屏幕上的奇怪位置:
这是一个众所周知的编辑器的例子(虽然没有用草稿实现),所以你可以更好地理解我想要实现的内容.
首先,这是触发建议弹出窗口的函数:
private onChange(editorState: EditorState) {
const content = editorState.getCurrentContent();
const selection = editorState.getSelection();
const currentBlock = content.getBlockForKey(selection.getAnchorKey());
if (selection.isCollapsed()) {
const blockText = currentBlock.getText();
const wordMeta = getWordAt(blockText, selection.getAnchorOffset());
const categoryRegex = /([\w]*)/;
const matches = wordMeta.word.match(categoryRegex);
const existingEntity = currentBlock.getEntityAt(wordMeta.begin);
if (!existingEntity && matches) {
const categorySearch = matches[1];
const selection = window.getSelection();
if (this.state.autoComplete.search !== categorySearch && selection.rangeCount > 0) {
const range = selection.getRangeAt(0); …Run Code Online (Sandbox Code Playgroud) function sel() {
document.getElementById('testid').focus();
document.getElementById('testid').setSelectionRange(10, 10);
}Run Code Online (Sandbox Code Playgroud)
<input style="width:50px" id="testid" value="abcdefghijklmnopqrst">
<button onclick="sel()">select 10,10</button>Run Code Online (Sandbox Code Playgroud)
input.setSelectionRange(10,10) 第一次不将值滚动到插入符号位置。滚动到插入符号是否可行?
javascript ×5
html ×3
jquery ×3
input ×2
autocomplete ×1
caret ×1
draftjs ×1
reactjs ×1
select ×1
textarea ×1
textinput ×1
typescript ×1