【问题标题】:Why new line is not applied when copying text to clipboard为什么将文本复制到剪贴板时不应用新行
【发布时间】:2019-12-16 20:00:47
【问题描述】:

我之前问过“How to make a share button to share quote with post URL in Google blogger blog”并得到了解决方案。

现在我正在尝试制作回退功能,因为大多数浏览器不支持 Web Share API 方法并提出了解决方案。

<script>
//<![CDATA[
var title = document.title;
var url = window.location.href;

document.querySelectorAll('.shareBtn').forEach(function (btn) {
    var text = btn.previousElementSibling.textContent + '\n';
    btn.addEventListener('click', function () {
        if (navigator.share) {
            navigator.share({
                title: title,
                text: text,
                url: url
            });
        }else{
                var shareText = document.createElement('input');
                document.body.appendChild(shareText)
                shareText.value = text+url;
                shareText.select();
                document.execCommand('copy',false);
                shareText.remove();
                alert(" Text Copied");
            }
    });
});
//]]>
</script>

在 if part var text = btn.previousElementSibling.textContent + '\n' 在 text 和 url 之间应用了行间距,但是当 else 部分执行时不应用行间距。

【问题讨论】:

    标签: javascript share blogger android-sharing


    【解决方案1】:

    您需要在createElement() 中使用textarea 元素而不是input

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多