我有这个JavaScript函数:
function maxLengthPaste(field,maxChars)
{
event.returnValue=false;
if((field.value.length + window.clipboardData.getData("Text").length) > maxChars) {
field.value = field.value + window.clipboardData.getData("Text").substring(0, maxChars - field.value.length);
return false;
}
event.returnValue=true;
}
该window.clipboardData.getData("Text")
在Chrome浏览器不能正常工作是否有任何跨浏览器的代码来代替它?
没有,没有对window.clipboardData
跨浏览器支持。它仅支持IE。对于window.clipboardData
支持通常被认为是一个安全问题,因为它可以让每个网站您访问阅读无论发生什么事在你的剪贴板中的时间。
在Chrome中,您可以处理粘贴事件时读clipboardData
:
document.addEventListener('paste', function (evt) {
console.log(evt.clipboardData.getData('text/plain'));
});
跨浏览器的方法应该是
document.addEventListener('paste', function (evt) {
clipdata = evt.clipboardData || window.clipboardData;
console.log(clipdata.getData('text/plain'));
});