问题
Built a feature that deselects all but the ctrl-clicked item. It works fine in IE and Chrome, but not in Firefox.
Try the jsfiddle at http://jsfiddle.net/PJVK3/
Tried to capture the same event with simple javascript code and the same problem occurs. This works in Chrome(24.0.1312.57) and IE(9), but not 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? anyone?
回答1:
If you take a look the source code you'll see that it stores the event on event.browserEvent
.
It's a fixed bug: https://github.com/highslide-software/highcharts.com/pull/992
So, why you have to use event.browserEvent
instead of window.event
?
Simple, because window.event
doesn't exist in 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;
}
}
}
demo
Source code - relevant code:
.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);
}
});
回答2:
you can use something like this:
var evtobj = window.event ? event : e;
if (evtobj.shiftKey) {
//do something
} else {
//other things
}
This will work in Chrome, IE and firefox.
来源:https://stackoverflow.com/questions/14888778/event-ctrlkey-works-in-ie-and-chrome-but-not-firefox-on-legenditemclick