简体   繁体   中英

How to copy a link address to the clipboard when a copy button is clicked?

My current solution does not work, could you help me figure out why?

This is my javascript:

$(function() {

var clip = new ZeroClipboard(document.getElementById("copy"));

clip.on("ready", function() 
{

clip.on("copy", function(event) {

var clipboard = event.clipboardData;

clipboard.setData(attr.('href'),$("#text-to-copy"));


});

});

});

The html I'm using:

<a id="text-to-copy" th:href="@{http://localhost:8080/{id}(id=${url})}"

th:text="@{/{id}(id=${url})}" data-clipboard-target="text-to-copy"></a>

<div class="row" style="padding-top: 20px; padding-left: 20px;">

<input type="button" class="btn btn-default btn-radio" id="copy" name="copy" 
value="copy" />

Well your supposed to use click() not copy.

$(function() {

    var clip = new ZeroClipboard(document.getElementById("copy"));

    clip.on("ready", function() {

        clip.click(function(event) {
            var clipboard = window.clipboardData;
            clipboard.setData(attr.('href'),$("#text-to-copy"));
        });
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM