zoukankan      html  css  js  c++  java
  • html js 执行粘贴无效和 判断选中的内容(纯文本和html)是否为空

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <style>
            .a {
                 100px;
                height: 100px;
                border: 1px solid;
            }
            img{
                 100px;
                height: 100px;
                border: 1px solid;
            }
        </style>
    </head>
    <body>
    <a href="mailto:name@email.com">Email</a>
    <div class="a"></div>
    <input type="text" value="123123">
    <button>dianwo</button>
    <div id="testDiv" contenteditable="true"><b>123123</b>456 <img src="a.jpg" alt=""></div>
    </body>
    <script>
    
        /**
         * 问题1
         * 粘贴无效
         */
        document.querySelector('button').onclick = function () {
            document.querySelector('input').focus()
            document.execCommand("Paste","false",null);
            console.log('执行粘贴')
        }
    
    
        /**
         * 
         * 判断选中的内容(纯文本和html)是否为空,
         */
        var testDiv = document.getElementById("testDiv");
    
        testDiv.onmouseup = function(){
    
            var selectionObj = null, rangeObj = null, selectedText = "", selectedHtml = "";
    
            if(window.getSelection){
    
                selectionObj = window.getSelection();
    
                selectedText = selectionObj.toString();
    
                rangeObj = selectionObj.getRangeAt(0);
    
                var docFragment = rangeObj.cloneContents();
    
                var tempDiv = document.createElement("div");
    
                tempDiv.appendChild(docFragment);
    
                selectedHtml = tempDiv.innerHTML;
    
            }else if(document.selection){
    
                selectionObj = document.selection;
    
                rangeObj = selectionObj.createRange();
    
                selectedText = rangeObj.text;
    
                selectedHtml = rangeObj.htmlText;
    
            }
    
            alert(selectedText);
    
            alert(selectedHtml);
    
        };
    </script>
    </html>
    

      参考内容:http://www.cnblogs.com/ArthurPatten/p/3317263.html

  • 相关阅读:
    Vue实现添加、删除、关键字查询
    打开新页面 自定义方法并获取携带值
    unity3d 刷新速率
    unity3d AssetStore 下载的资源位置
    unity3d c# http 请求json数据解析
    unity3d 自定义载入条/载入动画
    课程改进意见
    梦断代码
    An internal error occurred during: "Launching MVC on Tomcat 7.x".
    n以内的1的个数
  • 原文地址:https://www.cnblogs.com/WhiteHorseIsNotHorse/p/6952983.html
Copyright © 2011-2022 走看看