Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
428 views
in Technique[技术] by (71.8m points)

javascript - 如何在JavaScript中捕获右键单击事件? [重复](How can I capture the right-click event in JavaScript? [duplicate])

This question already has an answer here:(这个问题在这里已有答案:)

Is right click a Javascript event?(是否右键单击Javascript事件?) 16 answers(16个答案)

I want to block the standard context menus, and handle the right-click event manually.(我想阻止标准上下文菜单,并手动处理右键单击事件。)

How is this done?(这是怎么做到的?)   ask by Giffyguy translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use the oncontextmenu event.(使用oncontextmenu事件。)

Here's an example:(这是一个例子:) <div oncontextmenu="javascript:alert('success!');return false;"> Lorem Ipsum </div> And using event listeners:(并使用事件监听器:) el.addEventListener('contextmenu', function(ev) { ev.preventDefault(); alert('success!'); return false; }, false); Don't forget to return false, otherwise the standard context menu will still pop up.(不要忘记返回false,否则标准上下文菜单仍会弹出。) If you are going to use a function you've written rather than javascript:alert("Success!") , remember to return false in BOTH the function AND the oncontextmenu attribute.(如果你打算使用你编写的函数而不是javascript:alert("Success!") ,请记住在函数和oncontextmenu属性中返回false。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...