2012-02-20 4 views
2

나는 WYSIWYG 편집기를 만들고있어, 어떻게 캐럿 위치 작업을하는지 알아야합니다. 그것은 크로스 플랫폼을 수행하는 분명한 방법이없는 것으로 보인다.캐럿 포지션 크로스 브라우저?

구문이 필요합니다. Mozilla 개발자 페이지로 나를 안내하지 마십시오. 나는 특히 유용하다고 생각하지 않았다. 콘텐츠 편집 가능한 div를 사용하고 있습니다.

source that i looked at

답변

9

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); 
} 
+0

thks을 시도 ** 환호 ** – officegunner

+0

좋은 일 자스! +1 –

+0

Internet Explorer 10 이상에서 테스트 되었습니까? –