event.ctrlKey适用于IE和Chrome,但不适用Firefox legendItemClick
问题描述:
构建了一个功能,取消选择除ctrl单击的项目外的所有项目。它在IE和Chrome中运行良好,但不在Firefox中运行。
尝试jsfiddle在http://jsfiddle.net/PJVK3/event.ctrlKey适用于IE和Chrome,但不适用Firefox legendItemClick
试图用简单的javascript代码捕获相同的事件并发生同样的问题。 此作品在Chrome(24.0.1312.57)和IE(9),而在Firefox(18.0.2):
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction()
{
if (event.ctrlKey){document.getElementById("demo").innerHTML="ctl key pressed";}
if (!event.ctrlKey){document.getElementById("demo").innerHTML="ctl key NOT pressed";}
}
</script>
</head>
<body>
<p>Click the button to trigger a function.</p>
<button onclick="myFunction()">Click me</button>
<p id="demo"></p>
</body>
</html>
Whatup?任何人?
答
如果你看看源代码,你会发现它存储在event.browserEvent
上的事件。
这是一个固定的错误:https://github.com/highslide-software/highcharts.com/pull/992
那么,你为什么要使用event.browserEvent
,而不是window.event
?
很简单,因为window.event
在Firefox中不存在。
legendItemClick: function(e) {
var visibility = this.visible ? 'visible' : 'hidden';
if (!e.browserEvent.ctrlKey) {
if (!confirm('The series is currently '+
visibility +'. Do you want to change that?')) {
return false;
}
}
}
源代码 - 相关代码:
.on('click', function (event) {
var strLegendItemClick = 'legendItemClick',
fnLegendItemClick = function() {
item.setVisible();
};
// Pass over the click/touch event. #4.
event = {
browserEvent: event // < -- here is the magic
};
// click the name or symbol
if (item.firePointEvent) { // point
item.firePointEvent(strLegendItemClick, event, fnLegendItemClick);
} else {
fireEvent(item, strLegendItemClick, event, fnLegendItemClick);
}
});
答
你可以使用这样的事情:
var evtobj = window.event ? event : e;
if (evtobj.shiftKey) {
//do something
} else {
//other things
}
这将在Chrome浏览器,IE和Firefox浏览器。
NICE找到了,你赢了。这只是奇怪的,它只在Firefox中不起作用 – Ian 2013-02-15 06:34:17
你钉了它。不过,我确实相信这是一个错误。由于我托管自己的highcharts.js,所以它不适用于我的本地实现,因为我需要比标准托盘更多的颜色。我不得不下载最新版本才能正常工作。感谢一束。 – Zoom 2013-02-15 08:12:53