<TEXTAREA ID="holdtext" >
</TEXTAREA>
<input ID="holdtext1" type="text">
<BUTTON onClick="ClipBoard();">Copy to Clipboard</BUTTON>
<SCRIPT LANGUAGE="JavaScript">
function ClipBoard(){
Copied = holdtext.createTextRange();
Copied1 = holdtext1.createTextRange();
Copied.execCommand("RemoveFormat");
Copied.execCommand("Copy");
Copied1.execCommand("RemoveFormat");
Copied1.execCommand("Copy");
}
</SCRIPT>
//Here i am not able to copy both the fields.I am ale to copy from a single field but my form has multiple fields please help me out.
最佳答案
window.clipboardData.setData()对您可能很有趣:
window.clipboardData.setData('text',
document.getElementById('holdtext').value +
document.getElementById('holdtext1').value
);
如果您想使用textRange进行操作,则可以设置textRange的text-property:
function ClipBoard()
{
//create a new element, otherwise setting of range.text
//will change the form-fields values
var dummy=document.createElement('textarea');
//the range
Copied = dummy.createTextRange();
//set the text
Copied.text = document.getElementById('holdtext').value +
document.getElementById('holdtext1').value;
//expand the range to contain all contents
Copied.expand('textedit');
//useless, because textarea's contents doesn't apply formatting
Copied.execCommand("RemoveFormat");
//Copy
Copied.execCommand("Copy");
}
但请始终牢记:访问clipBoard可能会导致丢失数据,您永远不会知道clipBoard内部当前存在的内容。
您确实不应该从网页访问clipBoard。