簡體   English   中英

在HTML的同一元素中使用多個事件

[英]Using more than one event in the same element in HTML

我正在嘗試在用戶單擊按鈕並且/如果用戶按下Enter鍵時觸發功能。 我不確定如何在同一元素中存儲兩個事件。

 <td> <input type= "button" disabled id ="e2" value="Exercise 2" onclick ="loadQsets(2);setRadioValues(2);disableInput() ;" /></td>

如何在同一元素中同時使用onclick事件和Enter鍵事件來觸發相同功能?

您需要處理keydown事件並將邏輯放在if statement

參見示例。 13Enter鍵的密碼

 document.getElementById('inp').addEventListener('keydown', function(e){ if(e.keyCode === 13){ console.log('Enter is pressed !'); } }); 
 <input id="inp"> 

  <td> <input type= "button" disabled id ="e2" value="Exercise 2" onclick ="loadQsets(2);setRadioValues(2);disableInput() ;" /></td> 

 window.onload=function(){ var btn = document.getElementById('e2'); function handleStuff() { loadQsets(2); setRadioValues(2); disableInput(); } btn.onclick = function() { handleStuff(); }; btn.onkeydown = function() { handleStuff(); } } 
  <td> <input type= "button" disabled id ="e2" value="Exercise 2" /></td> 

暫無
暫無

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

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