문제

I have a div with "contenteditable = true".
For ex:
<div id="editableDiv" contenteditable="true">
<br/>fggggdf
</div>

How to get the character offset(for example character 'd' offset) with respect to the div "editableDiv".

Thanks in advance

도움이 되었습니까?

해결책

I bet this answer will help you.

I was thinking of a similar approach to the one presented here. Basically, you will have to do something like this:

It basically copies the whole text from inside the textarea into a div of the same size. I have set some CSS to ensure that in every browser, the textarea and the div wrap their content in exactly the same way.

다른 팁

Check this out. You can find the offset where mouse is clicked to show your suggestion box.

http://jsfiddle.net/8dZBT/2/

$('#editableDiv').click(function(e){
    var x = e.pageX;
    var y = e.pageY;
    x -= $(this).offset().left;
    y -= $(this).offset().top;
    alert(x+' '+y)

});
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top