2009-04-06 23 views

Trả lời

39

Bạn có thể xem thuộc tính shiftKey của sự kiện nhấp chuột.

window.addEventListener("click", 
 
    function(e) { 
 
    if (e.shiftKey) console.log("Shift, yay!"); 
 
    }, 
 
    false);
<p>Click in here somewhere, then shift-click.</p>

2

Sự kiện này bị sa thải từ DOM ought chứa một shiftKey (hoặc tương đương) bất động sản cho thấy trạng thái của phím shift khi sự kiện đã bị sa thải; xem, ví dụ: https://developer.mozilla.org/en/DOM/event.shiftKey cho một ví dụ.

Nếu bạn đang sử dụng thư viện gói JavaScript/DOM như YUI, Prototype hoặc jQuery, bất kỳ sự khác biệt nào trong triển khai phải không phải là vấn đề.

3

Bạn cần đảm bảo bạn vượt qua event làm tham số cho hàm onclick của mình. Bạn cũng có thể chuyển các tham số khác.

<html> 
<head> 
    <script type="text/javascript"> 
     function doSomething(event, chkbox) 
     { 
      if (event.shiftKey) 
       alert('Shift key was pressed while picking ' + chkbox.value); 
      else 
       alert('You picked ' + chkbox.value); 
     } 
    </script> 
</head> 
<body> 
    <h3>Pick a Color</h3> 
    <input type="radio" name="myColors" onclick="doSomething(event, this)" value="Red" /> Red<br/> 
    <input type="radio" name="myColors" onclick="doSomething(event, this)" value="Green" /> Green<br/> 
    <input type="radio" name="myColors" onclick="doSomething(event, this)" value="Blue" /> Blue<br/> 
</body> 
</html> 
Các vấn đề liên quan