如果实现复制一个链接或者文本,其实很简单的,但是有很多个要复制呢,这里我们需要用到for循环相关的技术知识,比如我们需要操作一下列表内容的复制,如下图:
然后分享下源码:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>多个复制</title> </head> <body> <input type="text" id = "url1" value = "http://www.baidu.com"><a href="javascript:(0)" id = "btn1">点我复制</a><br /> <input type="text" id = "url2" value = "http://www.jd.com"><a href="javascript:(0)" id = "btn2">点我复制</a><br /> <input type="text" id = "url3" value = "http://www.hshs.com"><a href="javascript:(0)" id = "btn3">点我复制</a><br /> <input type="text" id = "url4" value = "http://www.iii.com"><a href="javascript:(0)" id = "btn4">点我复制</a> <script src = "/demo/js/jq.js"></script> <script type="text/javascript"> var btn = []; var url = [] var len = $("input").size(); for (var i = 0; i < len; i++) { btn.push("#btn" + (i + 1)); url.push("#url" + (i + 1)); } console.log(btn); for(var j in btn){ (function(data_j){ $(btn[data_j]).bind("click", function () { $(url[data_j]).select(); document.execCommand("Copy"); }); })(j); } </script> </body>
OK,供大家学下咯
网友评论文明上网理性发言 已有0人参与
发表评论: