Jacklovely

导航

 
 1 <!DOCTYPE html>
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 5     <title>复制所有链接</title>
 6     <script type="text/javascript">
 7         onload = function () {
 8             var values = "";
 9             //这里可以用name得到所有标签
10             var ck = document.getElementsByName("name");
11             //这里用数组存放链接地址,也可以用字符串+=存放,但数组比较灵活
12             var arr = [];
13             btnCopy.onclick = function () {
14                 for (var i = 0; i < ck.length; i++) {
15                     if (ck[i].checked == true) {
16                         arr.push(ck[i].nextSibling.href);
17                     }
18                     //使用剪切板,arr不能直接用,要用join方法,或tostring方法
19                     clipboardData.setData("text", arr.join("\r\n"));
20                     //clipboardData.setData("text", arr.toString());
21                     //text.value = arr;
22                 }
23             }
24             //全选
25             btnAll.onclick = function () {
26                 for (var i = 0; i < ck.length; i++) {
27                     ck[i].checked = true;
28                 }
29             }
30 
31             //反选
32             btnExcept.onclick = function () {
33                 for (var i = 0; i < ck.length; i++) {
34                     ck[i].checked = !ck[i].checked;
35                 }
36             }
37         }
38 
39     </script>
40 </head>
41 <body>
42     <!--input标签和a标签要放到同一行,不然dom获取节点会获取到空节点-->
43     <input type="checkbox" name="name" value="" /><a href="http://www.baidu.com">葫芦娃</a><br />
44     <input type="checkbox" name="name" value="" /><a href="http://www.jd.com">葫芦娃</a><br />
45     <input type="checkbox" name="name" value="" /><a href="http://www.taobao.com">葫芦娃</a><br />
46     <input type="checkbox" name="name" value="" /><a href="http://www.qq.com">葫芦娃</a><br />
47     <input type="button" name="" value="全选" id="btnAll" />
48     <input type="button" name="" value="反选" id="btnExcept" />
49     <input type="button" name="" value="复制链接" id="btnCopy" /><br />
50     <textarea style="width:300px;height:200px;" id="text"></textarea>
51 
52 </body>
53 </html>

 

posted on 2016-04-14 22:29  Jacklovely  阅读(521)  评论(0编辑  收藏  举报