我希望能够点击某个点,并将该点的工具提示内容复制到浏览器剪贴板中。我在这里有一个妥协的解决方案,它将工具提示的html复制到输入字段中,然后将其复制到剪贴板中。理想情况下我喜欢这样做而不需要输入字段,因为这也需要将焦点移动到输入字段。
我认为应该可以选择工具提示本身的文本并从那里复制而不是将输入用作中间文件。
在此example点击某个点会复制工具提示的html
id name datecol RN
1 A 2016-11-11 1
2 B 2016-11-11 2
3 C 2016-11-11 3
11 A1 2016-11-12 1
14 D1 2016-11-13 1
16 F1 2016-11-14 1
17 G1 2016-11-14 2
答案 0 :(得分:0)
document.execCommand('texttocopy');
适用于大多数现代浏览器。尝试使用高图表工具提示执行它。
否则你总是可以使用Github的Zelo Clipboard(浏览器需要支持Flash才能使用此选项)
答案 1 :(得分:0)
有多种方法。但是我发现这个最安全,因为用户实际上必须这样做:
$(function() {
function copyToClipboard(text) {
window.prompt("Copy to clipboard: Ctrl+C, Enter", text);
}
$('#container').highcharts({
chart: {},
tooltip: {
useHTML: true,
borderWidth: 0,
style: {
padding: 0
}
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
series: [{
data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4],
events: {
click: function(event) {
console.log(this.chart.tooltip.label.div.textContent);
var theText = this.chart.tooltip.label.div.textContent;
copyToClipboard(theText);
}
}
}]
});
});
弹出框会显示要复制的文本,并提示执行此操作。
答案 2 :(得分:0)
我有一个可行的解决方案虽然仍然使用中间html元素,但它不再导致我在我发布的示例中看到的焦点问题的移动。
How do I copy to the clipboard in JavaScript?
中的copyTextToClipboard函数$(function() {
$('#container').highcharts({
chart: {},
tooltip: {
useHTML: true,
borderWidth: 0,
style: {
padding: 0
}
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
series: [{
data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4],
events: {
click: function(event) {
copyTextToClipboard(this.chart.tooltip.label.div.innerHTML);
}
}
}]
});
function copyTextToClipboard(text) {
var textArea = document.createElement("textarea");
//
// *** This styling is an extra step which is likely not required. ***
//
// Why is it here? To ensure:
// 1. the element is able to have focus and selection.
// 2. if element was to flash render it has minimal visual impact.
// 3. less flakyness with selection and copying which **might** occur if
// the textarea element is not visible.
//
// The likelihood is the element won't even render, not even a flash,
// so some of these are just precautions. However in IE the element
// is visible whilst the popup box asking the user for permission for
// the web page to copy to the clipboard.
//
// Place in top-left corner of screen regardless of scroll position.
textArea.style.position = 'fixed';
textArea.style.top = 0;
textArea.style.left = 0;
// Ensure it has a small width and height. Setting to 1px / 1em
// doesn't work as this gives a negative w/h on some browsers.
textArea.style.width = '2em';
textArea.style.height = '2em';
// We don't need padding, reducing the size if it does flash render.
textArea.style.padding = 0;
// Clean up any borders.
textArea.style.border = 'none';
textArea.style.outline = 'none';
textArea.style.boxShadow = 'none';
// Avoid flash of white box if rendered for any reason.
textArea.style.background = 'transparent';
textArea.value = text;
document.body.appendChild(textArea);
textArea.select();
try {
var successful = document.execCommand('copy');
var msg = successful ? 'successful' : 'unsuccessful';
console.log('Copying text command was ' + msg);
} catch (err) {
console.log('Oops, unable to copy');
}
document.body.removeChild(textArea);
}
});