想实现以下功能:
1、点击文本框,复制文本框内容。
2、如果复制成功,提示“复制成功”;
3、如果复制不成功,提示“当前浏览器不支持复制,请手动复制!”。以下是我的部分代码,请教如何修改:------------------------------------------------------------
<div align=left style="width:510px;height:330px;overflow-y:auto;border:1;"><table><?php
$xml_array=simplexml_load_file('9.xml'); //将XML中的数据,读取到数组对象中$xml = $xml_array->news->new;
$p = ($_POST["page"] - 1) * 3;
for($i=$p; $i<$p+3; $i++) {
?>
<tr>
<td height="70px" style="line-height:22px;BORDER: #5391EE 1px solid;" onMouseOver=this.style.backgroundColor='#DCECF4'; onMouseOut=this.style.backgroundColor=''>
<?php echo $xml[$i]->shortdesc;
?> </td>
</tr></table></div>
1、点击文本框,复制文本框内容。
2、如果复制成功,提示“复制成功”;
3、如果复制不成功,提示“当前浏览器不支持复制,请手动复制!”。以下是我的部分代码,请教如何修改:------------------------------------------------------------
<div align=left style="width:510px;height:330px;overflow-y:auto;border:1;"><table><?php
$xml_array=simplexml_load_file('9.xml'); //将XML中的数据,读取到数组对象中$xml = $xml_array->news->new;
$p = ($_POST["page"] - 1) * 3;
for($i=$p; $i<$p+3; $i++) {
?>
<tr>
<td height="70px" style="line-height:22px;BORDER: #5391EE 1px solid;" onMouseOver=this.style.backgroundColor='#DCECF4'; onMouseOut=this.style.backgroundColor=''>
<?php echo $xml[$i]->shortdesc;
?> </td>
</tr></table></div>
<script type="text/javascript">
function copyCode(id){
var testCode=document.getElementById(id).value;
if(copy2Clipboard(testCode)!=false){
alert("生成的代码已经复制到粘贴板,你可以使用Ctrl+V 贴到需要的地方去了哦! ");
}
}
copy2Clipboard=function(txt){
if(window.clipboardData){
window.clipboardData.clearData();
window.clipboardData.setData("Text",txt);
}
else if(navigator.userAgent.indexOf("Opera")!=-1){
window.location=txt;
}
else if(window.netscape){
try{
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
}
catch(e){
alert("您的firefox安全限制限制您进行剪贴板操作,请打开’about:config’将signed.applets.codebase_principal_support’设置为true’之后重试,相对路径为firefox根目录/greprefs/all.js");
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=txt;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>
<div>
<a href="#" target="_blank"> [<a href="#" onclick="copyCode('txtTestCode1');return false;">点击复制1</a>]<br>
<textarea rows=2 id="txtTestCode1" style="width:500px;font-family: Verdana, Arial;font-size:12px;height:55px;border:1px solid #999999;">复制内容1</textarea>
</div>
<br><br>
<div>
<a href="#" target="_blank"> [<a href="#" onclick="copyCode('txtTestCode2');return false;">点击复制2</a>]<br>
<textarea rows=2 id="txtTestCode2" style="width:500px;font-family: Verdana, Arial;font-size:12px;height:55px;border:1px solid #999999;">复制内容2</textarea>
</div>
<br><br>
<div>
<a href="#" target="_blank"> [<a href="#" onclick="copyCode('txtTestCode3');return false;">点击复制3</a>]<br>
<textarea rows=2 id="txtTestCode3" style="width:500px;font-family: Verdana, Arial;font-size:12px;height:55px;border:1px solid #999999;">复制内容3</textarea>
</div>
<br><br>楼主 参考下。
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">
function CopyText() {
var text = document.getElementById("div1").innerText;
window.clipboardData.clearData();
window.clipboardData.setData("Text", text);
}
</script>
<title></title>
</head>
<body>
<div id="div1" onclick="CopyText()">
abcd
efgh
</div>
</body>
</html>
function CopyValue(input)
{
var clipBoardContent="";
clipBoardContent+=input.value;
if(window.clipboardData){
window.clipboardData.clearData();
window.clipboardData.setData("Text", clipBoardContent);
}else if(navigator.userAgent.indexOf("Opera") != -1){
window.location = clipBoardContent;
}else if (window.netscape){
try{
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
}catch (e){
alert("您的当前浏览器设置已关闭此功能!请按以下步骤开启此功能!\n新开一个浏览器,在浏览器地址栏输入'about:config'并回车。\n然后找到'signed.applets.codebase_principal_support'项,双击后设置为'true'。\n声明:本功能不会危极您计算机或数据的安全!");
}
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 = clipBoardContent;
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);
}
alert("已成功复制!");
return true;
}
function copyfun(i)
{
var bv=document.getElementById(i);
CopyValue(bv);
}
</script>
<textarea name="text" cols="80" rows="2" id="text" onclick="copyfun('text')">1.这里发言,表示您接受了CSDN社区的用户行为准则</textarea>