2016-11-11 29 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文件到服务器时未插入。你有任何补充的酱汁来源吗? –