function selectText(x) { if (document.selection) { var range = document.body.createTextRange();//ie range.moveToElementText(x); range.select(); } else if (window.getSelection) { var selection = window.getSelection(); var range = document.createRange(); selection.removeAllRanges(); range.selectNodeContents(x); selection.addRange(range); } } function cp(x) { selectText(x); document.execCommand("copy"); layer.msg("已复制"); } function gettxt(obj) { show(obj); } function show(txt) { layer.open({ type: 1, title: "URL", closeBtn: 0, shadeClose: true, skin: 'yourclass', area: ['30%', 'auto'], content: "<p onclick='cp(this);'>"+txt+"</p>" }); }
使用方法:
直接在要复制的标签上添加click事件
onclick="gettxt($(this).attr('title'));"
onclick='cp(this);'