简体   繁体   English

我想让链接仅在按下修饰符时起作用

[英]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: 几件事情:

  1. 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. 如果您有意避免使用此类库,请确保进一步研究处理事件。

  2. 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()处理程序,可以使您获得更轻松,更好的结果。

  1. Why? 为什么? (curious) (好奇)
  2. You've already fired onclick using the A tag. 您已经使用A标签触发了onclick You can't also onclick from the function you called onclick . 你不能还onclick从你调用的函数onclick
  3. In order to reference e , you have to pass it in as an argument. 为了引用e ,您必须将其作为参数传递。
  4. You can't pass 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. 在控制台中,如果您在单击链接时按下任何一个键,则将看到truefalse

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.

相关问题 仅当在文本字段中键入正确的单词时,才能使链接有效? - How can I make a link work, only when a correct word is typed into a text field? 仅在定位标记链接时使用网页上的 Tab 按钮导航时,我想让空格键充当回车键单击 - While navigating using Tab button on a web page only when at a anchor tag link, I want to make spacebar act as a enter key click 按下按钮时如何使JavaScript工作? - How to make JavaScript work when button is pressed? 我想在按下按钮时显示元素。 默认情况下显示该元素,如何使其默认不显示? - I want to make an element display when a button is pressed. by default the element is shown, how do I make it display none by default? 我只想在 hover 并且它工作时才在图像上放置一个链接,但是当我在链接上使用 hover 时它开始滞后 - i want to put a link on the image only while hover and it worked , but when i hover over the link it start lagging 链接单击上的 Javascript 代码仅运行一次,我希望每次单击链接时都运行它 - Javascript code on link click runs only once, I want to get it run everytime when link is clicked js如何在按下链接时关闭导航栏? - js how to make navbar close when a link is pressed? ajax effects other select option change event( I want to make changes that change event work only when I select an item not when ajax changing it)) - ajax effects other select option change event( I want to make changes that change event work only when I select an item not when ajax changing it)) 我想在两个功能之间切换,这段代码只工作一次,但我想让它每次点击都工作 - i want to toggle between two functions this code works only one time but i want to make it work each time i click 如何使搜索仅在按 Enter 键时才起作用? - How I make the search only work when I hit enter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM