var oInput = document.createElement("input");
oInput.setAttribute("value",filePath);
oInput.setAttribute("id","copyInput");
oInput.setAttribute("display","none");
document.body.appendChild(oInput);
var copyInput = document.getElementById("copyInput");
copyInput.select();
document.execCommand("copy");
$("#copyInput").remove();
filePath是想要复制的内容.
但是需要注意,在ajax会调用无法用此方法。
These are the steps to follow when triggering copy, cut or paste actions through a scripting API:
- Execute the corresponding action synchronously.
- Use the action’s return value as the return value for the API call.
Note: Copy and cut commands triggered through a scripting API will only affect the contents of the real clipboard if the event is dispatched from an event that is trusted and triggered by the user, or if the implementation is configured to allow this. Paste commands triggered through a scripting API will only fire paste events and give access to clipboard contents if the implementation is configured to allow this. How implementations can be configured to allow read or write access to the clipboard is outside the scope of this specification.