簡體   English   中英

如何檢測在范圍輸入上按下的換檔鍵?

[英]How to detect shift key pressed on range input?

按下Shift鍵時,我試圖同步兩個滑塊。 但是,問題是未為范圍輸入事件定義e.shiftKey。

我嘗試使用onmousedown事件,該事件確實報告e.shiftKey,但未正確設置范圍值。

如何檢測范圍輸入上是否按下了Shift鍵?

 $('input[type=range]').on("input",function(e){ $('#value').text(this.value); if(e.shiftKey){ var newValue = this.value; $('input[type=range]').each(function(){this.value=newValue}); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <html> <body> <input type="range"> <input type="range"> <span id="value"></span> </body> </html> 

(jQuery是可選的,為方便起見僅使用它)

無論是否單擊范圍輸入,都應檢查shiftKey:

 var sync = false; $(document).on('keydown', function(e) { if (e.shiftKey) { sync = true; } }); $(document).on('keyup', function(e) { if (e.shiftKey == false) { sync = false; } }); $('input[type=range]').on("input",function(e){ $('#value').text(this.value); if (sync) { var newValue = this.value; $('input[type=range]').each(function(){this.value=newValue}); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <html> <body> <input type="range"> <input type="range"> <span id="value"></span> </body> </html> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM