js实现图片粘贴到网页

news/2024/7/19 14:36:33 标签: js图片粘贴, 图片粘贴, 图片复制, js

图片粘贴到网页中话不说直接上代码" style="border-bottom:1px solid rgb(234,236,239);line-height:1.25;font-size:1.75em;color:rgb(36,41,46);font-family:'-apple-system', BlinkMacSystemFont, '微软雅黑', 'PingFang SC', Helvetica, Tahoma, Arial, 'Hiragino Sans GB', 'Microsoft YaHei', SimSun, '宋体', Heiti, '黑体', sans-serif;">实现通过按下ctrl + v将粘贴板上的图片粘贴到网页中,话不说直接上代码

js" style="color:rgb(51,51,51);background-position:0px 0px;background-color:transparent;font-family:'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, Courier, monospace;font-size:11.9px;border:0px;line-height:inherit;">js-meta" style="font-weight:700;color:rgb(153,153,153);"><!DOCTYPE html>
js-tag" style="color:#000080;"><js-name">html js-attr" style="color:#008080;">lang=js-string" style="color:rgb(221,17,68);">"en">
js-tag" style="color:#000080;"><js-name">head>
    js-tag" style="color:#000080;"><js-name">meta js-attr" style="color:#008080;">charset=js-string" style="color:rgb(221,17,68);">"UTF-8">
    js-tag" style="color:#000080;"><js-name">title>图片粘贴js-tag" style="color:#000080;"></js-name">title>
    js-tag" style="color:#000080;"><js-name">style>
        js-selector-id" style="color:rgb(153,0,0);font-weight:700;">#img{
            js-attribute" style="color:#000080;">width: js-number" style="color:#008080;">500px;
        }
    js-tag" style="color:#000080;"></js-name">style>
    js-tag" style="color:#000080;"><js-name">img js-attr" style="color:#008080;">id=js-string" style="color:rgb(221,17,68);">"img" js-attr" style="color:#008080;">src=js-string" style="color:rgb(221,17,68);">"" js-attr" style="color:#008080;">alt=js-string" style="color:rgb(221,17,68);">"">
    通过Ctrl + v将图片粘贴
js-tag" style="color:#000080;"></js-name">head>
js-tag" style="color:#000080;"><js-name">body>
    js-tag" style="color:#000080;"><js-name">script>
        setPasteImg();
        js-comment" style="color:rgb(153,153,136);font-style:italic;">//获取粘贴板上的图片
        js-function">js-keyword" style="color:rgb(51,51,51);font-weight:700;">function js-title" style="color:rgb(153,0,0);font-weight:700;">setPasteImg(js-params">){
            js-comment" style="color:rgb(153,153,136);font-style:italic;">//粘贴事件
            js-built_in" style="color:rgb(0,134,179);">document.addEventListener(js-string" style="color:rgb(221,17,68);">'paste', js-function">js-keyword" style="color:rgb(51,51,51);font-weight:700;">function(js-params">event){
                js-keyword" style="color:rgb(51,51,51);font-weight:700;">if (event.clipboardData || event.originalEvent) {
                    js-keyword" style="color:rgb(51,51,51);font-weight:700;">var clipboardData = (event.clipboardData || event.originalEvent.clipboardData);
                    js-keyword" style="color:rgb(51,51,51);font-weight:700;">if(clipboardData.items){
                        js-keyword" style="color:rgb(51,51,51);font-weight:700;">var  blob;
                        js-keyword" style="color:rgb(51,51,51);font-weight:700;">for (js-keyword" style="color:rgb(51,51,51);font-weight:700;">var i = js-number" style="color:#008080;">0; i < clipboardData.items.length; i++) {
                            js-keyword" style="color:rgb(51,51,51);font-weight:700;">if (clipboardData.items[i].type.indexOf(js-string" style="color:rgb(221,17,68);">"image") !== js-number" style="color:#008080;">-1) {
                                blob = clipboardData.items[i].getAsFile();
                            }
                        }
                        js-keyword" style="color:rgb(51,51,51);font-weight:700;">var render = js-keyword" style="color:rgb(51,51,51);font-weight:700;">new FileReader();
                        render.onload = js-function">js-keyword" style="color:rgb(51,51,51);font-weight:700;">function (js-params">evt) {
                            js-comment" style="color:rgb(153,153,136);font-style:italic;">//输出base64编码
                            js-keyword" style="color:rgb(51,51,51);font-weight:700;">var base64 = evt.target.result;
                            js-built_in" style="color:rgb(0,134,179);">document.getElementById(js-string" style="color:rgb(221,17,68);">'img').setAttribute(js-string" style="color:rgb(221,17,68);">'src',base64);
                        }
                        render.readAsDataURL(blob);
                    }

                }

            })

        }

    js-tag" style="color:#000080;"></js-name">script>
js-tag" style="color:#000080;"></js-name">body>
js-tag" style="color:#000080;"></js-name">html>

演示结果



http://www.niftyadmin.cn/n/764547.html

相关文章

android 数据存储怎么保存图片_python爬虫25 | 爬取下来的数据怎么保存? CSV 了解一下...

大家好我是小帅b是一个练习时长两年半的练习生喜欢唱&#xff01;跳&#xff01;rap&#xff01;篮球&#xff01;敲代码&#xff01;装逼&#xff01;不好意思我又走错片场了接下来的几篇文章小帅b将告诉你如何将你爬取到的数据保存下来有文本文件、redis、数据库(MySQL、Mang…

python3 xml 对象转字符串_Python2和Python3的区别

pythonPython2和Python3分别是Python的两个版本&#xff0c;按照Python官方的计划&#xff0c;Python2只支持到2020年。为了不带入过多的累赘&#xff0c;Python3在设计的时候没有考虑向下相容&#xff0c;许多针对早期Python版本设计的程序都无法在Python3上正常执行。为了照顾…

Vue怒踩微信JS-SDK中配置的坑 - 签名错误

前言 这几天公司需要做微信端h5项目&#xff0c;需要用到微信扫一扫功能&#xff0c;就需要涉及到配置微信JS-SDK的问题&#xff0c;怎么配置怎么生成签名这边就不说了官方文档(https://mp.weixin.qq.com/wiki?tresource/res_main&idmp1421141115)都说的比较详细&#xf…

python 判断是否是缺失值_python缺失值可视化处理--missingno

##缺失值 empty_column [] for e, c in enumerate(df.columns):if sum(pd.isnull(df[c]))!0:empty_column.append(c)print("feature_no:%d t feature_name:%s t null_num:%d t null_rate: %.2f%%"% (e, c , sum(pd.isnull(df[c])), 100*sum(pd.isnull(df[c]))/len(d…

Vue单页面应用优化SEO之预渲染

这里优化SEO重点说vue-cli框架&#xff0c;毕竟是spa项目&#xff0c;所以SEO十分不友好&#xff0c;原因在于它自身并不是静态html&#xff0c;而是由js动态生成的(通过文档碎片劫持的方式以及Object.defineProperties等方法&#xff0c;有兴趣可以去研究研究vue的原理)&#…

后端如何发出请求_后端同学的前端学习之路

前言为什么需要前端学习&#xff1a;需要搭建一个完整的流程工具系统&#xff0c;而组内前端资源比较匮乏&#xff0c;因此前端的学习是必须的。总的来说&#xff0c;需要学习这三个基本的方面 第一章&#xff1a; 总体概述与概览 HTML 定义了网页的内容CSS 定义了网页的布局Ja…

微信移动端开发 - 账号登录授权

登录授权获取openid 过程&#xff1a; 通过appid以及一些其他参数拼接微信公众号的登录授权链接&#xff0c;通过所填写的回调地址会返回一个code参数回来&#xff0c;通过code向后台发起请求获取openid&#xff0c;此code只能使用一次&#xff0c;即获取一次openid后该code作…

python在浏览器运行_在web浏览器中执行脚本(python selenium)

网页中有一个脚本&#xff0c;允许您查看视频和其他内容。在 使用python和selenium不能在鼠标点击后运行脚本。使用此代码的当前am的&#xff1a;videoGemPath ".//*[idbrandconnect]/a/img" scriptPath ".//*[idcontent]/script" elementScript "…