我正在寫一個基於Web的代碼編輯器,其中每行是一個內容編輯DIV插入位置。 我寫了一個本地方法要在一個給定的索引插入符的位置,但它只能在Chrome中。歌劇和firefox總是設置插入行的插入符號。GWT設置內容編輯DIV
public native void setCursorPos(int index) /*-{
//console.log(index);
var that = [email protected]::getElement()();
var position = index;
var el = that;
var treeWalker = document.createTreeWalker(el, NodeFilter.SHOW_TEXT,
function(el) {
return NodeFilter.FILTER_ACCEPT;
}, false);
while (treeWalker.nextNode()) {
if (position - treeWalker.currentNode.length <= 0) {
var range = document.createRange();
var sel = window.getSelection();
console.log(position);
range.setStart(treeWalker.currentNode, position);
range.setEnd(treeWalker.currentNode, position);
range.collapse(true);
sel.removeAllRanges();
sel.addRange(range);
el.focus();
return;
} else {
position = position - treeWalker.currentNode.length;
}
}
同時測試代碼,我就是用這個http://jsbin.com/EcETajo/5/edit 並在鉻,FF和歌劇作品。
是函數錯了或什麼的GWT生成的代碼改變插入位置?
編輯:使用treewalker,因爲我想在未來的
EDIT2包裹在跨節點的文本處理文本hightlighting IM:OK,我已經通過我發現了一個問題。
變種範圍= document.createRange(); var sel = window.getSelection();
這些線路是錯誤的。在GWT的js代碼是在iframe中,所以要訪問我的HTML元素,我不得不使用這樣的事情 變種範圍= $ doc.createRange(); var sel = $ doc.getSelection(); wher $文檔是由GWT
設置一個變量
請與您的解決方案回答你的問題並接受它。 – Fedy2