JS 原生实现 Safari 浏览器下的复制

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/FengHongSeXiaoXiang/article/details/82824756

JS 原生实现 Safari 浏览器下的复制

var btn = document.querySelector('#btn');
btn.addEventListener('click',function(){
	var input = document.createElement('input');
    input.setAttribute('readonly', 'readonly');
    input.setAttribute('value', 'hello world');
    document.body.appendChild(input);
	input.setSelectionRange(0, 9999);
	if (document.execCommand('copy')) {
		document.execCommand('copy');
		console.log('复制成功');
	}
    document.body.removeChild(input);
});

猜你喜欢

转载自blog.csdn.net/FengHongSeXiaoXiang/article/details/82824756