js實現複製連結

進擊的趕海人發表於2019-11-30
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<script type="text/javascript">
    str = "http://www.baidu.com"
</script>
<body>
<div id="article" name="http://www.lanyife.com.cn/console/tougu-zhanye-menu/view?id=100000051" onClick='copyUrl()' value="複製連結">複製連結</div>

</body>
</html>
<script
        src="https://code.jquery.com/jquery-3.3.1.min.js"
        integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
        crossorigin="anonymous">
</script>
<script type="text/javascript">
    function copyUrl()
    {
        var clipBoardContent= document.getElementById("article").attributes["name"].value;
        let oInput = document.createElement('input');
        oInput.value = clipBoardContent;
        document.body.appendChild(oInput);
        oInput.select();
        document.execCommand("Copy");
        oInput.style.display = 'none';
        document.body.removeChild(oInput);
        window.alert('複製成功')
    }
</script>

成功輸出結果:

相關文章