正好项目中需要用到剪切板获取返回值,思路本来是想在后台将返回值放入剪切板中,一直没有成功,查了好多资料,问了不少人,都没有解决问题,具体代码如下:在项目中获取系统剪切板时Clipboard返回值一直为空,有知道的朋友可以帮我解答一下。
StringSelection stringSelection = new StringSelection("++++++");
// 系统剪贴板
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
System.out.println(clipboard);
clipboard.setContents(stringSelection, null);
}
}
重点来了:js 在ajax中不能执行剪切板操作,document.execCommand("Copy");返回值为false
具体代码:
<script >
function copyUrl2()
{
var oInput = document.createElement('input');
oInput.value = "我的名字";
document.body.appendChild(oInput);
oInput.select(); // 选择对象
document.execCommand("Copy"); // 执行浏览器复制命令
oInput.className = 'oInput'; //创建示例
oInput.style.display='none'; //将并将示例隐藏
alert('复制成功');
}
</script>
</head>
<body>
<div onClick="copyUrl2()" value="点击复制代码">点击复制代码</div>
<div onClick="copyUrl1()">oahduhefh</div>
</body>
执行代码,Ctrl+V 本应该粘贴显示为 :我的名字
但是在ajax中执行<script>中的代码 document.execCommand("Copy");返回值为false
说明没有成功,原因是ajax默认为异步执行,
可以在ajax中添加下面黄字内容:async:false, //将ajax设置为同步
$.ajax({
type : "POST",
url : "copyNode.do",
data : da,
async:false,
success : function(json) {
..........
}