<div class="us_textArea js_text js_textarea" contenteditable="true" onpaste="return pasteListener(event, true)"></div>
//文本域粘贴图片
function pasteListener(event, isLimit) {
$.each(event.clipboardData.items, function(index, item) {
if(item.kind == "file" && item.type.match("image/*")) {
var file = item.getAsFile();
if(isLimit && file.size > 2 * 1024 * 1024) {
// parent.index.layers("msg", "粘贴的图片内容过大,最大支持为2M。");
/*弱提示*/
layui.use('layer', function(){
var layer = layui.layer;
layer.msg('粘贴的图片内容过大,最大支持为2M。',{
time : 3000,
});
});
return true;
}
var fileReader = new FileReader();
fileReader.onload = function(event) {
var img = "<img src='" + event.target.result + "' style='" + (isLimit ? "max-width:200px;" : "") + "'" + (isLimit ? "" : "") + ">";
var selection = window.getSelection();
var range = selection.getRangeAt(0);
range.insertNode($(img)[0]);
selection.anchorOffset = selection.focusOffset;
selection.collapseToEnd();
}
fileReader.readAsDataURL(file);
} else if(item.kind == "string" && item.type.match("text/plain")) {
item.getAsString(function(str) {
var selection = window.getSelection();
var range = selection.getRangeAt(0);
range.insertNode(new Text(str));
selection.anchorOffset = selection.focusOffset;
selection.collapseToEnd();
});
}
});
return false;
}
版权声明:本文为qq_37103514原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。