如何在vanila javascript中的函数中看到event.target,该函数由;oninput“; 函数myFunction(事件){ console.log(event);//我可以使用此事件找到密钥码吗? } document.getElementById('i')。addEventListener(“按键键”),(e)=>{ console.log(e.which)});
对于初学者提出的问题,我很抱歉,但是我是否可以使用myFunction中的事件来查看按键代码,就像我将事件侦听器添加到下面的元素一样。我认为您无法在“oninput”事件中获取按键代码。。但你可以做到:如何在vanila javascript中的函数中看到event.target,该函数由;oninput“; 函数myFunction(事件){ console.log(event);//我可以使用此事件找到密钥码吗? } document.getElementById('i')。addEventListener(“按键键”),(e)=>{ console.log(e.which)});,javascript,html,events,Javascript,Html,Events,对于初学者提出的问题,我很抱歉,但是我是否可以使用myFunction中的事件来查看按键代码,就像我将事件侦听器添加到下面的元素一样。我认为您无法在“oninput”事件中获取按键代码。。但你可以做到: <input type="text" oninput="myFunction(event)" id="i"> function myFunction(event){ console.log(event); //ca
<input type="text" oninput="myFunction(event)" id="i">
function myFunction(event){
console.log(event); //can i find the keycode using this event?
}
document.getElementById('i').addEventListener("keypress", (e) => {
console.log(e.which)});
(from)我认为您无法在“oninput”事件中获取密钥代码。。但你可以做到:
<input type="text" oninput="myFunction(event)" id="i">
function myFunction(event){
console.log(event); //can i find the keycode using this event?
}
document.getElementById('i').addEventListener("keypress", (e) => {
console.log(e.which)});
(from)event.keyCode和event 下面是一个工作示例:
let keyCode;
input.addEventListener('keydown', (e) => {
keyCode = e.keyCode
});
input.addEventListener('input', (e) => {
console.log(keyCode)
})
document.getElementById('i')。addEventListener('keyup',(e)=>{
console.log(e.key);//将返回精确按下的键
});
event.keyCode和event
下面是一个工作示例:
let keyCode;
input.addEventListener('keydown', (e) => {
keyCode = e.keyCode
});
input.addEventListener('input', (e) => {
console.log(keyCode)
})
document.getElementById('i')。addEventListener('keyup',(e)=>{
console.log(e.key);//将返回精确按下的键
});
您可以使用:
<form>
<input type="text" id="i">
</form>
<script>
document.getElementById('i').addEventListener('keyup', (e) => {
console.log(e.key); // will return exact pressed key
});
</script>
而不是事件。目标
工作示例:
constshowkeycode=(event)=>console.log(event.key);
document.getElementById('i')。addEventListener(“按键”,showKeycode,false)代码>
您可以使用:
<form>
<input type="text" id="i">
</form>
<script>
document.getElementById('i').addEventListener('keyup', (e) => {
console.log(e.key); // will return exact pressed key
});
</script>
而不是事件。目标
工作示例:
constshowkeycode=(event)=>console.log(event.key);
document.getElementById('i')。addEventListener(“按键”,showKeycode,false)代码>
您需要keycode还是target?我猜是target。keycode位于target no中?target
是触发事件的元素。如果我将event.target或event.currentTarget放在一起,它们都为空。与keypress
事件不同,input
事件没有显示按下哪个键的数据。当输入的值
属性发生更改时,会触发该事件,且与键盘输入无关。应该用keydown
或keydup
替换。您需要keycode还是target?我猜是target。keycode位于target no中?target
是触发事件的元素。如果我将event.target或event.currentTarget放在一起,它们都为空。与keypress
事件不同,input
事件没有显示按下哪个键的数据。当输入的值
属性发生更改时,会触发该事件,且与键盘输入无关。并应替换为keydown
或keydup
。