我正在制作WYSIWYG编辑器,我需要知道如何让插入位置正常工作.似乎没有明显的跨平台方式.
我只需要语法.请不要指向Mozilla开发者页面; 我没觉得它特别有用.我正在使用内容可编辑的div.
试试这个
function doGetCaretPosition (oField) {
// Initialize
var iCaretPos = 0;
// IE Support
if (document.selection) {
// Set focus on the element
oField.focus ();
// To get cursor position, get empty selection range
var oSel = document.selection.createRange ();
// Move selection start to 0 position
oSel.moveStart ('character', -oField.value.length);
// The caret position is selection length
iCaretPos = oSel.text.length;
}
// Firefox support
else if (oField.selectionStart || oField.selectionStart == '0')
iCaretPos = oField.selectionStart;
// Return results
return (iCaretPos);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4088 次 |
最近记录: |