How to disable <a onclick> when the user holds control
Usually, when people click on a link, I have onclick bound to it. And then return false.
When people click with "control", they expect a new page to open up. Therefore, I want to ignore the o开发者_JAVA百科nclick AND/OR detect it. How do I do this?
The event
object has a "ctrlKey" boolean flag, so you can check that in your handler. It depends a little on your framework, but generally if your handler returns false
then you'll have "defeated" the click.
In IE, the event
object is a global (that is, a property of the "window" object). In other browsers, it's a parameter passed to the handler. A common idiom therefore is:
function clickHandler(theEvent) {
theEvent = theEvent || window.event;
// ...
}
You need to use the OnMouseDown
event.
You may hook the OnKeyPress event and check if the control key is being pressed. If so, raise a flag that is used on the OnClick. If the flag is up, then skip all the OnClik normal code and put, on the else do what you need.
精彩评论