[英]I want to make a link work only when modifiers are pressed
I'm trying to make a my link work only when Ctrl+Shift is pressed, otherwise I'd like it to do nothing. 我试图使我的链接仅在按Ctrl + Shift时可用,否则,我希望它什么也不做。 I can't seem to get it to work though.
我似乎无法使它正常工作。 Any ideas?
有任何想法吗?
<html>
<head>
<script type="text/javascript">
function keycheck(){
if (e.ctrlKey && e.shiftKey) {;
document.getElementById("hello").onclick = function() {
location.href='http://www.mylink.com/';
}
}
else {;
document.getElementById("hello").onclick = function() {
location.href='#';
}
}
}
</script>
</head>
<body>
<a href="#" onClick="keycheck()" id="hello">HELLO</a>
</body>
</html>
Couple of things: 几件事情:
Without the use of a library such as jQuery you're going to get inconsistent results from straight onClick handlers. 如果不使用jQuery之类的库,那么直接的onClick处理程序将导致不一致的结果。 Especially when it comes to accessing the event object.
特别是在访问事件对象时。 If you are avoiding such libraries on purpose then be sure to research handling events a little more.
如果您有意避免使用此类库,请确保进一步研究处理事件。
With the above caveat, your basic approach is to do the same condition you have but simply cancel the event behavior if it false (so they didn't have both keys pressed). 有了以上警告,您的基本方法是执行与您相同的条件,但是如果事件为假,则只需取消事件行为(这样就不会同时按下两个键)。 You don't have to do anything when it's true because that is the default behavior anyway.
当它为true时,您无需执行任何操作,因为无论如何这是默认行为。
Event handling and cancelling is one of the areas where libraries prove extremely useful so you may get easier and better results using one and binding an on()
or click()
handler from which you can event.preventDefault(); 事件处理和取消是库被证明非常有用的领域之一,因此使用一个库并绑定可以进行event.preventDefault()的
on()
或click()
处理程序,可以使您获得更轻松,更好的结果。
onclick
using the A tag. onclick
。 You can't also onclick
from the function you called onclick
. onclick
从你调用的函数onclick
。 e
, you have to pass it in as an argument. e
,您必须将其作为参数传递。 e
in as an argument, you want to reference event
e
作为参数传递,而是要引用event
This works 这有效
<script type="text/javascript">
function foo() {
console.log(event.ctrlKey);
console.log(event.shiftKey);
}
</script>
<a href="#" onclick="foo()">Foo
In the console, you'll see true
or false
if either of those keys are pressed at the time you click the link. 在控制台中,如果您在单击链接时按下任何一个键,则将看到
true
或false
。
In order to have this link go to a specific URL when they are both pressed: 为了使该链接在同时被按下时转到特定的URL:
<script type="text/javascript">
function foo() {
if ( event.ctrlKey && event.shiftKey) {
window.location.href="foo.htm";
}
}
</script>
<a href="#" onclick="foo()">Foo
HOWEVER , in some browsers, pressing SHIFT + click
opens the URL of a link in a new browser window. 但是 ,在某些浏览器中,按
SHIFT + click
可在新的浏览器窗口中打开链接的URL。 I can't think of a way to prevent that since it's a browser setting, which can't be controlled by JavaScript. 我想办法防止这种情况发生,因为它是浏览器设置,无法通过JavaScript进行控制。 What will happen in that case is that the current window will go forward to "foo.htm", but the link "#" will be opened in a new browser window.
在这种情况下,当前窗口将前进到“ foo.htm”,但是链接“#”将在新的浏览器窗口中打开。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.