gpt4 book ai didi

javascript - Ios Safari 的复制按钮

转载 作者:行者123 更新时间:2023-11-28 19:34:55 26 4
gpt4 key购买 nike

hi everyone, i want copy button in my mobile website and i have implement it with JS, it works fine for android but on ios text not copying, is there any way to copy textarea text on ios safari ?

this is my code which worked on android browser

var input  = document.getElementById("input_output");
var button = document.getElementById("copy-button");

button.addEventListener("click", function (event) {
event.preventDefault();
input.select();
document.execCommand("copy");
});

谢谢

最佳答案

您可以试试这个,它适用于 Ios 和其他浏览器。

button.addEventListener("click", function (event) {
event.preventDefault();
if (navigator.userAgent.match(/ipad|ipod|iphone/i)) {
var $input = $('#input_output');
$input.val();
var el = $input.get(0);
var editable = el.contentEditable;
var readOnly = el.readOnly;
el.contentEditable = true;
el.readOnly = false;
var range = document.createRange();
range.selectNodeContents(el);
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
el.setSelectionRange(0, 999999);
el.contentEditable = editable;
el.readOnly = readOnly;

var successful = document.execCommand('copy');
$input.blur();
var msg = successful ? 'successful ' : 'un-successful ';


}
else{

var copyTextarea = document.querySelector('#input_output');
copyTextarea.select();

var successful = document.execCommand('copy');
var msg = successful ? 'successful ' : 'unsuccessful';

}

});

关于javascript - Ios Safari 的复制按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39960212/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com