defcected; event.preventdefault()的好替代方案"对于内联JavaScript代码
<button type="submit" class="button-danger" onclick="if(!window.confirm('aaa'))event.preventDefault();">aaa</button>
这是我的原始代码。 phpstorm显示事件
全局变量已弃用。
我不想制作一个单独的功能来完成我想做的事情,但是我只希望该按钮的“ onClick”属性内置此内联版本。
正确使用jQuery的替代性会更好。任何建议将不胜感激。
编辑:链接的问题的答案可以单独函数。我想要一个内置版本的内联版本。我不想使我的代码很长。
<button type="submit" class="button-danger" onclick="if(!window.confirm('aaa'))event.preventDefault();">aaa</button>
This is my original code. PHPStorm is showing that event
global variable is deprecated.
I do not want to make a separate function to do what I want but I just want this inline version built into the "onclick" attribute of the button.
Alternative that uses JQuery correctly will be better. Any advice will be appreciated.
EDIT: the linked question has answers that make a separate function. I want an INLINE version that is built into the onclick. I don't want to make my code long.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,如果您想编写不错的可维护现代JavaScript,那么您真的不应该使用内联操件。他们有一个大量的问题通常只能参考全局标识符(例如
window.event.event
) )。我强烈建议使用JavaScript正确安装活动处理程序。如果这实际上不是您的选项,那么通过引用
参数
为Inline Handler来完成您想做的事情。第一个参数将是事件,因此参数[0] .preventDefault()
将执行您想要的工作。To start with, you really shouldn't use inline handlers if you want to be writing decent maintainable modern JavaScript. They have a plethora of problems including usually only being able to reference global identifiers (such as
window.event
). I'd strongly recommend attaching the event handler properly using JavaScript.If that's really not an option for you, it's just barely possible to do what you want by referencing the
arguments
for the inline handler. The first argument will be the event, soarguments[0].preventDefault()
will do what you want.