如何在按钮点击时复制URL?

时间:2016-12-04 12:28:10

标签: javascript php jquery html web

我正在尝试在按钮点击时复制URL。一些我如何尝试,但没有工作。 http://www.w3schools.com/code/tryit.asp?filename=FAF25LWITXR5

package org.example.christopher;

import org.apache.log4j.*;
import org.apache.log4j.xml.DOMConfigurator;


public class AsyncLogging{

private static Logger logger = Logger.getLogger(AsyncLogging.class.getPackage().getName());
private AsyncAppender asyncAppender = null;
private ConsoleAppender consoleAppender = null;

public AsyncLogging(){

try{
logger.setAdditivity(false);
asyncAppender = (AsyncAppender) logger.getAppender("ASYNC");
asyncAppender.setBufferSize(128);
}
catch (Exception e){
System.out.println("error: " + e.toString());
}

}

public void doLogging(){
logger.debug("Debug 1");
logger.debug("Debug 2");
logger.debug("Debug 3");
//logger.debug("Debug 4");
//logger.debug("Debug 5");
}


public static void main(String ... args){
AsyncLogging asyncLogging = new AsyncLogging();
asyncLogging.doLogging();
}

}
function Copy() 
{
    var Url = document.createElement("textarea");
    Url.innerHTML = window.location.href;
    Copied = Url.createTextRange();
    Copied.execCommand("Copy");
}

5 个答案:

答案 0 :(得分:4)

无需创建新的textarea。尝试通过提供一些textarea(' url')来获取现有id

这是工作示例



function Copy() {
  var Url = document.getElementById("url");
  Url.innerHTML = window.location.href;
  console.log(Url.innerHTML)
  Url.select();
  document.execCommand("copy");
}

<div>
  <input type="button" value="Copy Url" onclick="Copy();" />
  <br /> Paste: <textarea id="url" rows="1" cols="30"></textarea>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

    <html>
<head>
    <title></title>
</head>
<script type="text/javascript">
            function Copy() 
            {
                //var Url = document.createElement("textarea");
                urlCopied.innerHTML = window.location.href;
                //Copied = Url.createTextRange();
                //Copied.execCommand("Copy");
            }
        </script>
<body>
    <div>

        <input type="button" value="Copy Url" onclick="Copy();" />
        <br />

        Paste: <textarea id="urlCopied" rows="1" cols="30"></textarea>
    </div>
</body>
</html>

答案 2 :(得分:0)

单击按钮时,选择#url的内容,然后将其复制到剪贴板。

<html>
  <body>
    <input type="button" value="Copy Url" id="copy" />
    <br />
    Paste: <textarea rows="1" cols="30" id="url"></textarea>
    <script type="text/javascript">
    document.querySelector("#copy").onclick = function() {
      document.querySelector("#url").select();
      document.execCommand('copy');
    };
    </script>
  </body>
</html>

答案 3 :(得分:0)

另一种解决方案,不需要额外的html代码:

<script>
    $(document).ready(function () {
        $(document).on("click", "#ShareButton", function (e) {
            $("body").append('<input id="copyURL" type="text" value="" />');
            $("#copyURL").val(window.location.href).select();
            document.execCommand("copy");
            $("#copyURL").remove();            
        });
    });
</script>

答案 4 :(得分:0)

 function Copy() 
            {
                    //var Url = document.createElement("textarea");
                    urlCopied.innerHTML = window.location.href;
                    //Copied = Url.createTextRange();
                    //Copied.execCommand("Copy");
            }
<html>
    <head>
        <title></title>
    </head>
    <body>
        <div>
            <input type="button" value="Copy Url" onclick="Copy();" />
            <br />
          
            Paste: <textarea id="urlCopied" rows="1" cols="30"></textarea>
        </div>
    </body>
</html>