我正在尝试使用以下代码执行复制到剪贴板的操作,该代码在桌面浏览器中运行良好,但当涉及移动浏览器(如 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/