将文本框的内容复制到剪贴板(javascript)
摘自:http://hi.baidu.com/kelly19850818/blog/item/33037b73473b321e8601b085
<script language="javascript"
type="text/javascript">
function copyToClipboard(txtID) {
if(window.clipboardData) {
} else if(navigator.userAgent.indexOf("Opera") != -1) {
window.location = txtValue;
} else if (window.netscape) {
try {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
} catch (e) {
alert("您的firefox安全限制限制您进行剪贴板操作,请打开'about:config'将signed.applets.codebase_principal_support'设置为true'之后重试");
return false;
}
var clip =
Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
if (!clip)
return;
var trans =
Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
if (!trans)
return;
trans.addDataFlavor('text/unicode');
var str = new Object();
var len = new Object();
var str =
Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
var copytext = txtValue;
str.data = copytext;
trans.setTransferData("text/unicode",str,copytext.length*2);
var clipid = Components.interfaces.nsIClipboard;
if (!clip)
return false;
clip.setData(trans,null,clipid.kGlobalClipboard);
}
}
</script>
在模版页面因为没有head必须把JS代码放到最底下,不然会找不到JS脚本。
<input id="Button3" type="button" value="复制到剪贴板"
onclick="copyToClipboard('ctl00_ContentPlaceHolder1_txtIFrame')"/>
服务器端控件js会无法找到....
摘自:http://blog.chinaunix.net/u/21783/showart.php?id=150065
<script
language="javascript">
function ClipBoard()
{
document.getElementByIdx_x("holdtext").innerText=copytext.innerHTML;
Copide=document.getElementByIdx_x("holdtext").createTextRange();
Copide.execCommand("Copy");
}
</script>
<textarea id="holdtext"
style="display:none;">
</textarea>
<span id="copytext">text to be
copied</span><br
/>
<button
onclick="ClipBoard();">copy</button>
由此:http://www.csdndoc.cn/ForumsView/t/20030819/14/2163884.html经开心云衫实践应用改编如下:
(1)复制abc内容
<script>
window.clipboardData.setData('Text/html','abc')
</script>
也可:
<script
language="javascript">
function copyToClipBoard(){
var clipBoardContent='';
clipBoardContent+="复制到剪切板,这就是指定内容!!";
window.clipboardData.setData("Text",clipBoardContent);
}
copyToClipBoard();
</script>
(2)复制body里的所有有效代码,包括javascript,html等
<script>
clipboardData.setData('text',document.body.innerHTML);
</script>
(3)复制body里的可见文本,包括按钮等上面的文字及用style="display:none"设为隐的内容等。
<script>
clipboardData.setData('text',document.body.innerText);
</script>
(4)复制demo里面的内容
<textarea
id="demo">yourText</textarea>
<input type="button" value="setData"
onclick="demo.select();document.execCommand('Copy')">
摘自:http://www.0733168.com/Html/124/124101/2006-5/20060506114900.htm
<Script Language=Jscript>
alert(clipboardData.getData('text')) //取得当前的剪切版内容
window.clipboardData.setData('text','指定数据') //自定义剪切版中的内容
alert(clipboardData.getData('text')) //在次取得当前剪切版内容
</Script>