最近有个需求是判断按住ctrl,点击元素时进行多选,
问题是:
如何判断ctrl是否按下,查阅了一些资料,发现判断e的某些属性即可
e.ctrlKey ctrl按住时为true
e.shiftKey shift按住时为true
e.metaKey win按住时为true,mac下win键是command
复制代码
在对应的事件中判断e的某些属性即可:
document.addEventListener('click',(e)=>{
console.log('e.ctrlKey',e.ctrlKey) //判断ctrl是否按下
console.log('e.shiftKey',e.shiftKey) //判断shift是否按下
console.log('e.metaKey',e.metaKey) //判断win键是否按下(mac上是command)
})
复制代码
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END