简体   繁体   English

在uiwebview的contenteditable div中设置光标位置

[英]set cursor position in contenteditable div in uiwebview

I am loading UIWebView with html file. 我正在用HTML文件加载UIWebView。 the html file contains some text in Div tags. html文件在Div标签中包含一些文本。 I need to set cursor position in a div tag. 我需要在div标签中设置光标位置。 the html file contains different div tags.based on some condition i need to locate cursor position. html文件包含不同的div标签。基于某些条件,我需要定位光标位置。

This is helpful for setting the caret position. 这有助于设置插入标记的位置。

Suppose my html is like this: 假设我的html是这样的:

<div id="editable" contenteditable="true">
  text text text<br>text text text<br>text text text<br>
</div>
<button id="button" onclick="setCaret()">focus</button>

and my javascript method is: 而我的javascript方法是:

function setCaret() {
    var el = document.getElementById("editable");
    var range = document.createRange();
    var sel = window.getSelection();
    range.setStart(el.childNodes[2], 5);
    range.collapse(true);
    sel.removeAllRanges();
    sel.addRange(range);
    el.focus();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM