我忘記了跨瀏覽器兼容性的那一刻,我只是想讓它工作。 我在做的是試圖修改一個腳本(你可能不需要知道這一點)位於typegreek.com基本腳本被發現here。基本上它是當你輸入字符時,它將你輸入的字符轉換成希臘字符並將它打印到屏幕上。我試圖做的是讓它在contentEditable div的工作(它只適用於Textareas)需要將光標位置設置爲contentEditable div的末尾,問題與選擇和範圍對象
我的問題是與這一個功能:用戶鍵入一個鍵,它得到的轉換爲希臘鍵,和進入一個函數,它通過一些if進行排序,並且它結束的地方是我可以添加div支持的地方。這是我到目前爲止,
myField是div,myValue是希臘字符。
//Get selection object...
var userSelection
if (window.getSelection) {userSelection = window.getSelection();}
else if (document.selection) {userSelection = document.selection.createRange();}
//Now get the cursor position information...
var startPos = userSelection.anchorOffset;
var endPos = userSelection.focusOffset;
var cursorPos = endPos;
//Needed later when reinserting the cursor...
var rangeObj = userSelection.getRangeAt(0)
var container = rangeObj.startContainer
//Now take the content from pos 0 -> cursor, add in myValue, then insert everything after myValue to the end of the line.
myField.textContent = myField.textContent.substring(0, startPos) + myValue + myField.textContent.substring(endPos, myField.textContent.length);
//Now the issue is, this updates the string, and returns the cursor to the beginning of the div.
//so that at the next keypress, the character is inserted into the beginning of the div.
//So we need to reinsert the cursor where it was.
//Re-evaluate the cursor position, taking into account the added character.
var cursorPos = endPos + myValue.length;
//Set the caracter position.
rangeObj.setStart(container,cursorPos)
現在,只要我輸入的文字不超過原始文本的大小,它就會有效。說我在手邊有30個字符。如果我輸入的數字多於30,它會添加字符31,但將光標放回到30.我可以在pos.31鍵入字符32,然後在pos.32鍵入字符33,但如果我嘗試將字符34放入,它會添加字符,並將光標恢復爲32.問題是,如果cursorPos大於該範圍中定義的值,則用於添加新字符的函數會掛起。有任何想法嗎?
Bravo!它完美的工作! – DavidR 2010-05-31 17:42:40
我有一個類似的例子,發現'range.collapse(false)'比'setStart' +'setEnd'更合適,但是您建議使用'sel.removeAllRanges'後跟'sel.addRange'證明_very_有用在WebKit瀏覽器上! +1 – 2014-05-15 15:39:53
只是想說謝謝,經過努力使其工作2小時後發現這個 – 2016-09-01 09:54:58