Joh*_*ohn 11 javascript svg raphael
当用户右键单击使用Rapahel.js创建的元素时,我需要做出响应.我读到你应该只附加一个click事件处理程序,然后决定用户点击哪个鼠标按钮.我不能让这个工作.
<!DOCTYPE html>
<html>
<head>
<script src="http://raphaeljs.com/raphael.js"></script>
<script>
window.onload = function() {
var r = Raphael('demo', 640, 480);
r.rect(10, 10, 400, 400).attr({fill: 'red'}).click(function(){
alert('test');
});;
};
</script>
</head>
<body>
<div id="demo"></div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
只有在单击鼠标左键时才会显示警告框.有关如何在单击右键时显示警告框的任何建议?
我用jQuery看过这个小技巧:
$(r.rect(10, 10, 400, 400).attr({fill: 'red'}).node).bind('contextmenu', function(){
alert('test');
});
Run Code Online (Sandbox Code Playgroud)
但也请阅读此评论:
是的,这也有效,但只有jQuery,最好不要使用.node,因为Raphael有时会重新创建节点 - 所以你丢失了你的事件处理程序.
mih*_*hai 11
你可以使用mousedown
和测试e.which
.这适用于FF和Chrome:
var r = Raphael('demo', 640, 480);
r.rect(10, 10, 400, 400).attr({fill: 'red'}).mousedown(function(e){
if (e.which == 3)
alert("hi")
});
Run Code Online (Sandbox Code Playgroud)
对于跨浏览器解决方案,您可能还需要查看e.button
:http:
//www.quirksmode.org/js/events_properties.html#button