我的开发环境:
- .NET 4.5
- 带有 Razor 引擎的 ASP.NET MVC4
- jQuery
以下是我为使 Copy to Clipboard 能够跨 5 个浏览器工作所做的工作:
- 法拉盛 23
- IE 10
- 铬 29
- Safari 5.1.7
- 歌剧 16
我的场景:我要复制到剪贴板的文本是生成的,并与 html 中断 (br) 一起放入 Div。对于副本,我需要删除这些 html 中断并将它们替换为 /r/n。副本是通过按钮单击。
不是这可能不是最好的编码方式,但它对我有用。
从github获取最新的 ZeroClipboard
在我的 .cshtml 文件中,我定义了按钮和 div 并包含 ZeroClipboard.min.js 文件。
<!-- language-all: lang-html -->
<button id="btCopyToClipboard" name="btCopyToClipboard" type="button">Copy To Clipboard</button>
<div id="Basket" ></div>
Javascript部分:
<!-- language: lang-js -->
<script type="text/javascript">
$(document).ready(function () {
//"style" the buttons
$("#btCopyToClipboard").button();
//ZeroClipboard code
var clip = new ZeroClipboard(document.getElementById('btCopyToClipboard'), {
moviePath: "/Scripts/ZeroClipboard.swf", // URL to movie
trustedOrigins: null, //null Page origins that the SWF should trust (single string or array of strings)
hoverClass: "", // The class used to hover over the object
activeClass: "", // The class used to set object active
allowScriptAccess: "always", //sameDomain SWF outbound scripting policy
useNoCache: true, // Include a nocache query parameter on requests for the SWF
forceHandCursor: true //false Forcibly set the hand cursor ("pointer") for all glued elements
});
//if just using var clip = new ZeroClipboard(); then need to use .glue(..)
//clip.glue(document.getElementById('btCopyToClipboard'));
clip.on('load', function (client, args) {
DebugLog("ZeroClipboard.swf is loaded and user's flash version is: " + args.flashVersion);
});
//The complete event is fired when the text is successfully copied to the clipboard.
clip.on('complete', function (client, args) {
//alert("clip.onComplete(..) -- Copied text to clipboard args.text: " + args.text);
});
clip.on('mouseover', function (client) {
// alert("mouse over");
});
clip.on('mouseout', function (client) {
//alert("mouse out");
});
clip.on('mousedown', function (client) {
//alert("mouse down");
});
clip.on('mouseup', function (client) {
//alert("mouse up");
});
clip.on('dataRequested', function (client, args) {
//get text from basket
var txt = $("#Basket").html();
//to make Notepad honour line breaks, we have to do some magic
var windowsText = txt.replace(/\n/g, '\r\n');
//replace html break with line breaks
windowsText = windowsText.replace(/<br\s*\/?>/g, "\r\n");
client.setText(windowsText);
});
clip.on('noflash', function (client, args) {
var msg = "You don't support flash, therefore the Copy To Clipboard will not work.";
DebugLog(msg);
alert(msg);
});
clip.on('wrongflash', function (client, args) {
var msg = 'Your flash is too old ' + args.flashVersion + '. The Copy To Clipboard supports version 10 and up.';
DebugLog(msg);
alert(msg);
});
function DebugLog(message) {
if (console && console.log) {
console.log(message);
}
}
});//eof $(document).ready
</script>