js將內容複製到剪切板(最簡單的實現方式)

    content:爲要設成的文本
    // 創建元素用於複製
    const aux = document.createElement('input')
    // 獲取複製內容
    const content = v
    // 設置元素內容
    aux.setAttribute('value', content)
    // 將元素插入頁面進行調用
    document.body.appendChild(aux)
    // 複製內容
    aux.select()
    // 將內容複製到剪貼板
    document.execCommand('copy')
    // 刪除創建元素
    document.body.removeChild(aux)

此時內容會被剪切到剪切板

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章