2016-11-11 31 views
0
包圍創建的圖像

如何通過將execCommand

<input type="button" onclick="document.execCommand('insertimage',null,'http://barwoncopiers.com/wp-content/uploads/2011/03/linkedin-icon-45x45.jpg');" value="Img"/> 
 
<br> 
 
<div contenteditable="true" class="editor" id="editor"></div> 
 

 
--- 
 

 
<qwe><img></qwe>

如果創建的形象,我想自動附上標籤。

如何< qwe>標籤創建?

回答

0

您可以使用下面來測試執行的是真實的

if (document.execCommand('insertimage', null, 'http://barwoncopiers.com/wp-content/uploads/2011/03/linkedin-icon-45x45.jpg')) 

,以及在編輯框中位置的光標,單擊按鈕,注意if語句是如何用標籤包圍你的圖片(見檢查工具)執行

document.getElementById("input").onclick = function() { 
 
    if (document.execCommand('insertimage', null, 'http://barwoncopiers.com/wp-content/uploads/2011/03/linkedin-icon-45x45.jpg')) { 
 
    var images = document.querySelectorAll("img[src$='wp-content/uploads/2011/03/linkedin-icon-45x45.jpg']"); 
 
    for (var x = 0; x < images.length; ++x) { 
 
     if (images[x].parentNode.tagName != "QWE") { 
 
     images[x].outerHTML = "<qwe>" + images[x].outerHTML + "</qwe>"; 
 
     } 
 
    } 
 
    } 
 
}
<input id="input" type="button" value="Img" id="in" /> 
 
<br> 
 
<div contenteditable="true" class="editor" id="editor">hh</div>

+0

圖像上傳HTML文件到服務器時未插入。你有任何補充的醬汁來源嗎? –