<div id="textareaBox" style=" 500px;height: 300px; border: 1px solid #dcdcdc" contenteditable="true"></div>
document.getElementById("textareaBox").addEventListener('paste', function (e) {
var that = this,ev = e || event;
if (ev.clipboardData || ev.originalEvent) {
var clipboardData = (ev.clipboardData || ev.originalEvent.clipboardData);
if (clipboardData.items) {
var blob;
for (var i = 0; i < clipboardData.items.length; i++) {
if (clipboardData.items[i].type.indexOf("image") !== -1) {
blob = clipboardData.items[i].getAsFile();
}
}
var render = new FileReader();
render.onload = function (evt) {
//输出base64编码
var base64 = evt.target.result;
var imgElem = document.createElement('img');
var pElem = document.createElement('p');
imgElem.src = base64;
that.appendChild(pElem.appendChild(imgElem));
};
if (blob) {
render.readAsDataURL(blob);
}
}
}
});