美文网首页
cocoscreator网页cope,复制到粘贴板,直接调用就可

cocoscreator网页cope,复制到粘贴板,直接调用就可

作者: 浪荡少年 | 来源:发表于2020-09-07 13:26 被阅读0次

    /**

         * 复制网页字符串

         */

        public static webCopyString(str,errFunc:Function,successFunc:Function) {

            var input = str;

            const el = document.createElement('textarea');

            el.value = input;

            el.setAttribute('readonly', '');

            el.style.contain = 'strict';

            el.style.position = 'absolute';

            el.style.left = '-9999px';

            el.style.fontSize = '12pt'; // Prevent zooming on iOS

            const selection = getSelection();

            var originalRange = false;

            if (selection.rangeCount > 0) {

                originalRange = selection.getRangeAt(0);

            }

            document.body.appendChild(el);

            el.select();

            el.selectionStart = 0;

            el.selectionEnd = input.length;

            var success = false;

            try {

                success = document.execCommand('copy');

            } catch (err) {

                errFunc(err);

                console.error(err);

            }

            document.body.removeChild(el);

            if (originalRange) {

                selection.removeAllRanges();

                selection.addRange(originalRange);

            }

            successFunc();

            return success;

        }

    相关文章

      网友评论

          本文标题:cocoscreator网页cope,复制到粘贴板,直接调用就可

          本文链接:https://www.haomeiwen.com/subject/vqnkektx.html