首页 > 编程语言 >Is there an alternative to deprecated e.which in JavaScript?

Is there an alternative to deprecated e.which in JavaScript?

时间:2024-10-27 08:49:39浏览次数:8  
标签:function JavaScript deprecated button there mousemove alternative property event

题意:"在 JavaScript 中,有没有可以替代已弃用的 `e.which` 的方法?"

问题背景:

Im new to JavaScript event handling, I would like to trigger an event upon mousemove and left-click on a div element. My current implementation is to check that e.which == 1 when I trigger the mousemove event function. However, I have read that the e.which property is now deprecated (UIEvent: which property - Web APIs | MDN). My code:

"我是 JavaScript 事件处理的新手,我想在鼠标移动和左键点击一个 div 元素时触发一个事件。当前的实现是通过在触发 `mousemove` 事件时检查 `e.which == 1`。然而,我读到 `e.which` 属性已经被弃用(UIEvent: which property - Web APIs | MDN)。我的代码如下:"

div.addEventListener("mousemove", myEventFunction)

function myEventFunction(e){
    if (e.which == 1){
       //do something
    }

}

Is there any alternative to perform this operation?

"是否有替代方法来执行此操作?"

问题解决:

You can use event.button if it is gonna be a mouse event.

“如果是鼠标事件,你可以使用 `event.button`。”

The MouseEvent.button read-only property indicates which button was pressed on the mouse to trigger the event.

function myEventFunction(e) {
    e = e || window.event;
    if ("buttons" in e) {
        return button;
    }
    var button = e.which || e.button;
    return button;
}

The above function returns the button value.

“上述函数返回的是 `button` 值。”


标签:function,JavaScript,deprecated,button,there,mousemove,alternative,property,event
From: https://blog.csdn.net/suiusoar/article/details/143017257

相关文章

  • Loading class `com.mysql.jdbc.Driver’. This is deprecated. The new driver class
    原文链接:Loadingclass`com.mysql.jdbc.Driver’.Thisisdeprecated.Thenewdriverclassis`com.mysql.cj.jdbc.Driver’.–每天进步一点点(longkui.site)某日构建springboot项目时,报的错:Loadingclass`com.mysql.jdbc.Driver'.Thisisdeprecated.Thenewdriver......
  • There is no getter for property named ‘xxxxx’ in ‘class com.xxx.xx.xx.xxxx'”
    原文链接:Thereisnogetterforpropertynamed‘xxxxx’in‘classcom.xxx.xx.xx.xxxx’”,–每天进步一点点(longkui.site) 0.背景SpringMVC架构,使用mybatis执行insert语句,然后开始报错:org.mybatis.spring.MyBatisSystemException:nestedexceptionisorg.apache.......
  • c++基本介绍——std::holds_alternative()的基本介绍
    今天的工作中开发一个新功能,涉及到判断std::variant类型是否等于某个特定的值。就此机会学习一下,std::variant类型和调用std::holds_alternative进行持有值的检查。std::holds_alternative是C++17中引入的标准库函数,用于检查std::variant是否持有特定类型的值。它返回......
  • Error: There was a timeout while attempting to connect to the network at undefin
     trufflemigrate--networksepolia报错Error:Therewasatimeoutwhileattemptingtoconnecttothenetworkatundefined.Checktoseethatyourproviderisvalid.Ifyouhaveaslowinternetconnection,tryconfiguringalongertimeoutin......
  • pyttsx3 and its alternatives
    pyttsx3https://github.com/nateshmbhat/pyttsx3效果太差。pyttsx3isatext-to-speechconversionlibraryinPython.Unlikealternativelibraries,itworksoffline.✨FullyOFFLINEtexttospeechconversion......
  • [ECharts] There is a chart instance already initialized on the dom.
    [ECharts]Thereisachartinstancealreadyinitializedonthedom.  报错解释:这个错误表示在同一个DOM元素上已经初始化了一个ECharts图表实例,而你又尝试去在同一个DOM上初始化另一个图表实例。ECharts不允许在同一个DOM上叠加多个实例。解决方法:   在尝试初始化新......
  • 易优CMS为何我安装完提示这个报错?:Array and string offset access syntax with curly
    当你遇到类似 Arrayandstringoffsetaccesssyntaxwithcurlybracesisdeprecated 的报错时,通常是因为当前使用的PHP版本较高,而程序代码中使用了一些已弃用的语法。原因分析PHP版本过高:当前使用的PHP版本(如PHP7.4或更高版本)不再支持某些旧的语法形式。代码使......
  • 解决 Nodejs 中的“Punycode Module is Deprecated”问题
    大家好,我叫asimkhan,目前是metamelon的全栈开发人员。最近,我在为naseebi.com(一个婚姻移动和web应用程序)项目工作时遇到了一个令人沮丧的问题。该问题涉及node.js中punycode模块的弃用,我想与您分享我的经验和解决方案。问题在应用程序中使用配置文件创建功能时,我......
  • .NetCore MySqlException 多线程中(There is already an open DataReader associated w
    问题描述:其实标题只是遇到问题的其中之一,遇到三种异常信息如下:Lockwaittimeoutexceeded;tryrestartingtransaction大概意思:超过锁定等待超时;尝试重新启动事务 ThereisalreadyanopenDataReaderassociatedwiththisConnectionwhichmustbeclosedfirst.大......
  • 为何我安装完提示这个报错?:Array and string offset access syntax with curly braces
    错误信息 Arrayandstringoffsetaccesssyntaxwithcurlybracesisdeprecated 表明你在使用的PHP版本较高,而你的程序代码中使用了一些在较新版本中已弃用的语法。具体来说,这是PHP7.4及以上版本对数组和字符串偏移量访问语法 {} 的弃用警告。解决方案1.降低PHP......