gpt4 book ai didi

javascript - Object.keys(KeyboardEvent) 只获取一个 "isTrusted"键而不是所有键

转载 作者:行者123 更新时间:2023-12-01 00:23:14 26 4
gpt4 key购买 nike

<!DOCTYPE html>
<html>
<body>

<p>A function is triggered when the user is pressing a key in the input field.</p>

<input type="text" onkeydown="myFunction(event)">

<script>
function myFunction(event)
{
console.log(Object.keys(event))
console.log(event)
}
</script>

</body>
</html>

我在 https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onkeydown 中运行上面的代码我希望获得 MDN 中记录的所有 KeyboardEvent 对象属性:https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent

我的目标是get the property of the difference between two objects in javascript比较两个 KeyboardEvent 对象,但链接的解决方案不适用于 KeyboardEvent 对象,Object.keys(KeyboardEvent)只获取一个“isTrusted” key 而不是所有 key 。

最佳答案

Object.keys only iterate over whose enumerable property is true mozilla doc

function myFunction(event){

console.log(Object.keys(event))
console.log(event)

for (const property in event) {

if(typeof event[property] == "function"){
console.log(`${property}: `+':its a fuction');
}else{
console.log(`${property}: ${event[property]}`+': its a property');
}
}
}
<!DOCTYPE html>
<html>
<body>

<p>A function is triggered when the user is pressing a key in the input field.</p>

<input type="text" onkeydown="myFunction(event)">

</body>
</html>

关于javascript - Object.keys(KeyboardEvent) 只获取一个 "isTrusted"键而不是所有键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59235864/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com