javascript各种复制代码收集
<input type="button" name="anniu1" onClick='copyToClipBoard()' value="复制专题地址和url地址,传给QQ/MSN上的好友">
<script language="javascript">
function copyToClipBoard(){
var clipBoardContent="";
clipBoardContent+=document.title;
clipBoardContent+="";
clipBoardContent+=this.location.href;
window.clipboardData.setData("Text",clipBoardContent);
alert("复制成功,请粘贴到你的QQ/MSN上推荐给你的好友");
}
</script>
<br />
<br />
<span>直接复制url</span><br>
<input type="button" name="anniu2" onClick='copyUrl()' value="复制URL地址">
<script language="javascript">
function copyUrl()
{
var clipBoardContent=this.location.href;
window.clipboardData.setData("Text",clipBoardContent);
alert("复制成功!");
}
</script>
<span>点击文本框时,复制文本框里面的内容</span><br>
<input onClick="oCopy(this)" value="你好.要copy的内容!">
<script language="javascript">
function oCopy(obj){
obj.select();
js=obj.createTextRange();
js.execCommand("Copy")
alert("复制成功!");
}
</script>
<br />
<br />
<span>复制文本框或者隐藏域中的内容</span><br>
<script language="javascript">
function CopyUrl(target){
target.value=myimg.value;
target.select();
js=myimg.createTextRange();
js.execCommand("Copy");
alert("复制成功!");
}
function AddImg(target){
target.value="[IMG]"+myimg.value+"[/ img]";
target.select();
js=target.createTextRange();
js.execCommand("Copy");
alert("复制成功!");
}
</script>
<input name=myimg type=hidden id=myimg value="http://www.jb51.net" />
<input name=imgurl type=text size=32 value="http://www.jb51.net" />
<input type=button value="点击这里复制本站地址" onClick="CopyUrl(imgurl);" />
<br>
<br />
<span>复制span标记中的内容</span>
<script type="text/javascript">
</script>
<br />
<br />
<script type="text/javascript">function copyText(obj)
{
var rng = document.body.createTextRange();
rng.moveToElementText(obj);
rng.scrollIntoView();
rng.select();
rng.execCommand("Copy");
rng.collapse(false);
alert("复制成功!");
}
</script>
以下是代码片段:<br />
<br />
<span id="tbid">http://www.jb51.net</span>
[<a href="#" onClick="copyText(document.getElementById('tbid'))">点击复制</a>]
<span id="tbid2">http://www.jb51.net</span>
[<a href="#" onClick="copyText(document.getElementById('tbid2'))">点击复制</a>]