javascript - 复制到剪贴板时如何避免移动浏览器中的页面滚动

标签 javascript jquery html clipboarddata

我正在尝试使用以下代码执行复制到剪贴板的操作,该代码在桌面浏览器中运行良好,但当涉及移动浏览器(如 IOS safari/chrome)时,页面会在 $temp.val 处向下滚动(copydatacode).select();

 $('#myelementcopy').click(function(event){         
        var copydatacode= $('.addelement').html();
        var $temp = $("<input>");
        $("body").append($temp);
        $temp.val(copydatacode).select(); // when the code execute at this line the mobile browser jumps to the page bottom                     
        document.execCommand("copy");           
        $temp.remove(); 
        return false;
        event.preventDefault();         
 });

最佳答案

我正在寻找一个完美的解决方案,但现在我不是附加到 body 而是附加到被点击的元素。这会导致小滚动,而不是一直滚动到页面底部。在 jQuery 中,我使用了:

$(this).append($temp);

关于javascript - 复制到剪贴板时如何避免移动浏览器中的页面滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45624546/

相关文章:

html - Flash 重叠 HTML。 IE 中的 CSS 问题?

HTML 和 CSS 定位

javascript : how to send request in restful

php - Laravel - 防止错误显示在控制台中

javascript - 将数组参数传递给 XMLHttpRequest 对象

jquery - 日期在 jquery 中显示不正确

javascript - 页面加载后显示 div 2 秒

javascript - jQuery 表,可点击下拉列表 - 多个隐藏行?

javascript - 具有多种方法的 Controller 中的 Angular 服务注入(inject)

javascript - 如何更改 D3.js 中路径的颜色?