2012-07-04 20 views
1

在IE(7,8,9)中有div:<div contenteditable="true"></div>,然後點擊一個按鈕以使用document.execCommand('insertImage')方法插入圖像。然後插入圖像。到現在爲止沒有問題。 但插入的圖像與可調整大小的處理程序一起選擇。我如何取消選擇並重新調整圖像背後的焦點? 謝謝。如何在IE瀏覽器中插入圖片時如何重新聚焦?

$button.click(function(){ document.execCommand("insertImage",false,'url'); });

+0

怎麼樣的代碼? – silentw

+1

如果你有一個你想關注的元素的ID,你可以做$(「#elementId」)。focus() – Dan

+0

@ErmSo:我認爲你沒有正確地閱讀過這個問題。 –

回答

5

更新了簡單的代碼

下面是一些討厭的代碼來做到這一點。在其他瀏覽器中,它似乎仍然可以工作,因此可以按照原樣使用以下內容。

演示:http://jsfiddle.net/timdown/mr7Ac/6/

代碼:

function insertImage() { 
    var sel = document.selection; 
    if (sel) { 
     var textRange = sel.createRange(); 
     document.execCommand('insertImage', false, "someimage.png"); 
     textRange.collapse(false); 
     textRange.select(); 
    } else { 
     document.execCommand('insertImage', false, "someimage.png"); 
    } 
} 
+0

問題解決。謝謝。我創建了一個Range,但我沒有使用select()方法來使其集中。謝謝! –

+0

非常好! ..... – Viney

0

嘗試這樣的事情(即只):

function insertImage(){ 
    if(document.selection){ 
     var sel=document.selection; 
     var range=sel.createRange(); 
     var amount=(document.addEventListener)?2:1; 
     document.execCommand('insertImage', false, 'url'); 
     range.move('character',amount); 
     range.select(); 
    } 
    return; 
} 
+0

有趣但哈克,特別是IE 9嗅探。 –

相關問題