修复分享功能 - 添加复制链接和分享内容

// 分享功能
function suiyuShare(url) {
    var shareUrl = url || window.location.href;
    if (navigator.share) {
        navigator.share({
            title: document.title,
            url: shareUrl
        }).catch(function() {
            copyToClipboard(shareUrl);
        });
    } else {
        copyToClipboard(shareUrl);
        alert('✅ 链接已复制');
    }
}

function copyToClipboard(text) {
    if (navigator.clipboard) {
        navigator.clipboard.writeText(text);
    } else {
        var textarea = document.createElement('textarea');
        textarea.value = text;
        document.body.appendChild(textarea);
        textarea.select();
        document.execCommand('copy');
        document.body.removeChild(textarea);
    }
}

 

标签: 技术分享, 笔札

85 次阅读
分享文章
扫码打开文章

本文为原创文章,采用「署名—非商业性使用—相同方式共享 4.0 国际」许可协议(CC BY-NC-SA 4.0)。 转载请注明出处。

相关文章

添加新评论