我现在用的是Box2dJS库,它使用的原型,所以我必须包括为原型库它工作。我没有在我的主脚本中使用任何原型代码,只有Box2d对象使用原型。我在我的脚本中使用了JQuery,并且由于我包含了原型,所以jQuery事件似乎并没有触发。
下面是相关的JS代码:
$.noConflict();
jQuery(document).ready(function(){
init();
});
function init(){
//add event handler to surrounding DIV to monitor mouse move and keystrokes
jQuery("#container").keypress(function(e){
keys[e.keyCode] = true;
alert("key pressed!");
});
jQuery("#container").keydown(function(e){
keys[e.keyCode] = true;
});
jQuery("#container").keyup(function(e){
keys[e.keyCode] = true;
alert("Key released!");
});
}
这里是html代码:
<html>
<head>
<title>Box2D Test</title>
<!--[if IE]><script src="lib/excanvas.js"></script><![endif]-->
<script src="lib/prototype-1.6.0.2.js"></script>
<!-- box2djs scripts here-->
</head>
<body>
<center>
<div id="container" style="border:1px solid; cursor:none; width:600px; height:400px;">
<canvas id="canvas" width="600" height="400" >
Could not create Canvas!
</canvas>
</div>
<script type="text/javascript" src="jquery-1.6.2.min.js"></script>
<script src='js/box2dutils.js'></script>
<script type="text/javascript" src="main.js"></script>
</center>
</body>
</html>
警报都不要去了,但一切工作都OK,当我删除原型(和box2d)库。我怎样才能解决这个问题?
@Nicklas A.并非如此。我不是试图从jQuery中触发一个原型事件,我试图只使用jQuery事件。而且,这个问题甚至没有解决。 – Keelx