div position in html,Get caret HTML position in contenteditable DIV

问题

I am having troubles figuring out how to get caret position in a DIV container that contains HTML tags.

I am using this JavaScript function to do that:

function getCaretPosition()

{

if (window.getSelection && window.getSelection().getRangeAt)

{

var range = window.getSelection().getRangeAt(0);

var selectedObj = window.getSelection();

var rangeCount = 0;

var childNodes = selectedObj.anchorNode.parentNode.childNodes;

for (var i = 0; i < childNodes.length; i++)

{

if (childNodes[i] == selectedObj.anchorNode)

{

break;

}

if(childNodes[i].outerHTML)

{

rangeCount += childNodes[i].outerHTML.length;

}

else if(childNodes[i].nodeType == 3)

{

rangeCount += childNodes[i].textContent.length;

}

}

return range.startOffset + rangeCount;

}

return -1;

}

However, it finds a caret position of the text in my DIV container, when I need to find the caret position including HTML tags.

For example:

Text goes here along with some HTML tags.
;

(please note, that HTML tags are normal tags and are not displayed on the screen when the function is returning caret position)

If I click right between H and TML, the aforementioned function will find caret position without any problems. But I am getting the contents of DIV box in HTML format (including all tags), and if I want to insert something at that caret's position, I will be off by a few or many characters.

I went through many posts, but all I could find is either caret postions, or functions similar to what I have posted. So far I still cannot find a solution to get a caret position in a text that has HTML formatting.

Can anyone help, please?

PS. Here's JQuery/Javascript code that I wrote for the link button:

$('#pageEditor').on('click', '.linkURL', function()

{

var cursorPosition;

cursorPosition = getCaretPosition();

var contentID = $(this).parent().parent().attr('id');

var userSelected = getSelectionHtml();

var checkLink = userSelected.search('');

var anchorTag = 0;

if(checkLink == -1)

{

var currentContents = $('#'+contentID+' .peCont').html();

var indexOfSelection = currentContents.indexOf(userSelected);

var getPossibleAnchor = currentContents.slice(indexOfSelection, indexOfSelection+userSelected.length+6);

anchorTag = getPossibleAnchor.search('');

}

if(checkLink > 0 || anchorTag > 0)

{

//alert(checkLink);

document.execCommand('unlink', false, false);

}

else

{

$('#'+contentID+' .peCont').append('

Please enter URL for the link:
');

$('#linkEntry').dialog({

buttons: {

"Ok": function()

{

var attribute = $('#urlLink').val();

var newContentWithLink = '';

if(attribute != '')

{

if(userSelected != '')

{

var currentContent = $('#'+contentID+' .peCont').html();

var replacement = ''+userSelected+'';

newContentWithLink = currentContent.replace(userSelected, replacement);

}

else

{

var currentTextContent = $('#'+contentID+' .peCont').html();

var userLink = ''+attribute+'';

if(cursorPosition > 0)

{

var contentBegin = currentTextContent.slice(0,cursorPosition);

var contentEnd = currentTextContent.slice(cursorPosition,currentTextContent.length);

newContentWithLink = contentBegin+userLink+contentEnd;

}

else

{

newContentWithLink = attribute+currentTextContent;

}

}

$('#'+contentID+' .peCont').empty();

$('#'+contentID+' .peCont').html(newContentWithLink);

}

$(this).dialog("close");

} },

closeOnEscape:true,

modal:true,

resizable:false,

show: { effect: 'drop', direction: "up" },

hide: { effect: 'drop', direction: "down" },

width:460,

closeText:'hide',

close: function()

{

$(this).remove();

}

});

$('#linkEntry').on('keypress', function(urlEnter)

{

if(urlEnter.which == 13)

{

var attribute = $('#urlLink').val();

var newContentWithLink = '';

if(userSelected != '')

{

var currentContent = $('#'+contentID+' .peCont').html();

var replacement = ''+userSelected+'';

newContentWithLink = currentContent.replace(userSelected, replacement);

}

else

{

var currentTextContent = $('#'+contentID+' .peCont').html();

var userLink = ''+attribute+'';

if(cursorPosition > 0)

{

var contentBegin = currentTextContent.slice(0,cursorPosition);

var contentEnd = currentTextContent.slice(cursorPosition,currentTextContent.length);

newContentWithLink = contentBegin+userLink+contentEnd;

}

else

{

newContentWithLink = attribute+currentTextContent;

}

}

$('#'+contentID+' .peCont').empty();

$('#'+contentID+' .peCont').html(newContentWithLink);

$(this).dialog("close");

}

});

}

});

回答1:

I created a simple fiddle for you that does what you want.

This should work in recent versions of Firefox, Chrome and Safari.

It's your homework to add Opera and IE support if you need.

来源:https://stackoverflow.com/questions/11015313/get-caret-html-position-in-contenteditable-div

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值