Click button copy to clipboard using jQuery

前端 未结 21 2062
醉话见心
醉话见心 2020-11-21 23:11

How do I copy the text inside a div to the clipboard? I have a div and need to add a link which will add the text to the clipboard. Is there a solution for this?

         


        
21条回答
  •  有刺的猬
    2020-11-22 00:14

    html code here

        some example text
        
        
    

    JS CODE:

         function copyToClipboard(elementId) {
    
                          // Create a "hidden" input
                          var aux = document.createElement("input");
    
                          aux.setAttribute("value", document.getElementById(elementId).value);
                          // Append it to the body
                          document.body.appendChild(aux);
                          // Highlight its content
                          aux.select();
                          // Copy the highlighted text
                          document.execCommand("copy");
                          // Remove it from the body
                          document.body.removeChild(aux);
                        }
    

提交回复
热议问题