Ros*_*ose 7 html javascript clipboard clipboard.js
我正在使用clipboard.js,需要通过单击按钮来复制跨度中的文本.有没有办法做到这一点?
HTML:
<span id="spanId">text here</span>
<input type="button" class="buttonClass" value="Copy" data-clipboard-target="#spanId" />
Run Code Online (Sandbox Code Playgroud)
解决方案可以是:
// create a new instance of Clipboard plugin for the button element
// using the class selector: .buttonClass
var clipboard = new Clipboard('.buttonClass');
// when text is copied into clipboard use it
clipboard.on('success', function(e) {
$('#log').text('Text copied into clipboard is: <' + e.text + '>');
e.clearSelection();
});Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/1.5.10/clipboard.min.js"></script>
<span id="spanId">text here</span>
<input type="button" class="buttonClass" value="Copy" data-clipboard-target="#spanId"/>
<p id="log"></p>Run Code Online (Sandbox Code Playgroud)