更新:我已更新代码段,以包含Firebug中的整个页面。
我有以下代码:
<html><head>
<title>Welcome to CodeIgniter</title>
<!--[if IE]>
<script type="text/javascript" src="/assets/js/Jit/Extras/excanvas.js"></script>
<![endif]-->
<script src="/assets/js/jquery/jquery-1.4.2.min.js" type="text/javascript"></script>
<script src="/assets/js/Jit/jit.js" type="text/javascript"></script>
<script src="/assets/js/tree.js" type="text/javascript"></script>
<script src="/assets/js/jquery/jquery.rightClick.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$(".node").rightClick(function() {
alert ("RIGHT CLICK");
});
$.getJSON("/ajax/fetch/tree", function(data) {
init(data);
});
});
$(".node").live("click", function() {
alert ($(this).attr("id"));
});
</script>
<style type="text/css">
html, body {
width:100%;
height:100%;
background-color:#444;
text-align:center;
overflow:hidden;
font-size:9px;
font-family:Verdana, Geneva, Arial, Helvetica, sans-serif;
margin:0;padding:0;
}
/* visualization style container */
#infovis {
background-color:#222;
position:relative;
width:900px;
height:500px;
}
a, a:link, a:visited {
color:#343439;
}
/* spacetree nodes CSS class */
.node {
background-color:transparent;
font-weight:bold;
overflow:hidden;
text-decoration:none;
position:absolute;
text-align:center;
padding:4px 1px 1px 1px;
}
.node:hover {
color:#393434;
}
.hidden{
display:none;
}
</style>
</head><body>
<div id="infovis">
<div id="mycanvas" style="width: 900px; height: 500px; position: relative;">
<canvas id="mycanvas-canvas" width="900" height="500" style="position: absolute; top: 0pt; left: 0pt; width: 900px; height: 500px;"></canvas>
<div id="mycanvas-label" style="overflow: visible; position: absolute; top: 0pt; left: 0pt; width: 900px; height: 0pt;">
<div id="1" class="node" style="position: absolute; width: 60px; height: 17px; cursor: pointer; color: rgb(51, 51, 51); font-size: 0.8em; text-align: center; padding-top: 3px; left: 200px; top: 265px;">0.0</div>
<div id="4" class="node" style="position: absolute; width: 60px; height: 17px; cursor: pointer; color: rgb(51, 51, 51); font-size: 0.8em; text-align: center; padding-top: 3px; left: 420px; top: 215px;">2.0</div>
<div id="5" class="node" style="position: absolute; width: 60px; height: 17px; cursor: pointer; color: rgb(51, 51, 51); font-size: 0.8em; text-align: center; padding-top: 3px; left: 420px; top: 240px;">2.1</div>
<div id="9" class="node" style="position: absolute; width: 60px; height: 17px; cursor: pointer; color: rgb(51, 51, 51); font-size: 0.8em; text-align: center; padding-top: 3px; left: 420px; top: 265px;">2.2</div>
<div id="10" class="node" style="position: absolute; width: 60px; height: 17px; cursor: pointer; color: rgb(51, 51, 51); font-size: 0.8em; text-align: center; padding-top: 3px; left: 420px; top: 290px;">2.3</div>
<div id="2" class="node" style="position: absolute; width: 60px; height: 17px; cursor: pointer; color: rgb(51, 51, 51); font-size: 0.8em; text-align: center; padding-top: 3px; left: 310px; top: 253px;">1.0</div>
<div id="6" class="node" style="position: absolute; width: 60px; height: 17px; cursor: pointer; color: rgb(51, 51, 51); font-size: 0.8em; text-align: center; padding-top: 3px; left: 640px; top: 275px; display: none;">3.0</div>
<div id="3" class="node" style="position: absolute; width: 60px; height: 17px; cursor: pointer; color: rgb(51, 51, 51); font-size: 0.8em; text-align: center; padding-top: 3px; left: 310px; top: 278px;">1.1</div>
<div id="8" class="node" style="position: absolute; width: 60px; height: 17px; cursor: pointer; color: rgb(51, 51, 51); font-size: 0.8em; text-align: center; padding-top: 3px; left: 530px; top: 228px;">5.0</div>
<div id="11" class="node" style="position: absolute; width: 60px; height: 17px; cursor: pointer; color: rgb(51, 51, 51); font-size: 0.8em; text-align: center; padding-top: 3px; left: 530px; top: 253px;">5.1</div>
</div>
</div>
</div>
<div class="node">HELLO WORLD!</div>
</body></html>
我希望能够右键单击其中一个带有“node”类的div,然后触发一个事件。有没有办法用jQuery做到这一点?我查看了在http://www.javascripttoolbox.com/lib/contextmenu/找到的上下文菜单jQuery插件。但是,如果我使用$('node')。contextMenu(...)它不起作用。如果我使用$('infovis')。contextMenu(...),则上下文菜单可以正常工作。
看起来这些div实际上在画布之外。我直接从Firebug中提取了这段代码。
答案 0 :(得分:6)
试试这个:
$(document).ready(function(){
$(document)[0].oncontextmenu = function() {return false;}
$("#infovis").delegate('.node','mousedown',function(e){
if( e.button == 2 ) {
e.stopPropagation()
alert('Right mouse button!');
return false;
} else {
return true;
}
});
});
我认为#infovis从一开始就是可见的,你使用javascript用.node div填充该div。因此,您希望使用事件委派,因此您不会绑定到每个节点,而是绑定父节点。这使得您的代码更快,即使您有1000个.nodes。此外,您不需要将绑定放在回调中。
如果按钮== 2(右键单击按钮),我们委托mousedown捕获事件。
此外,您还可以通过停止contextmenu事件来禁用默认的右键单击行为。
请告诉我这是否适合您。
答案 1 :(得分:1)
您是否尝试过jquery.rightClick插件?似乎我做了一个快速测试:
http://abeautifulsite.net/blog/2008/05/jquery-right-click-plugin/
$('.node').rightClick(function() {
console.log('right click');
});
看起来它已经很老了,但那里的代码并不多。我认为可能需要稍微更新,但不多。
答案 2 :(得分:0)
试试这段代码:
$("#infovis").delegate('.node', 'mousedown', function(e) {
if (e.button == 2) {
e.stopPropagation()
console.log(e.target.id);
console.log(e.target.textContent);
return false;
} else {
return true;
}
});`
e.target
返回包含class = "node"
e.taget.textcontext
返回div
元素