jQuery:mouseup和mousedown点击处理程序

时间:2012-09-07 18:48:19

标签: jquery

我正在尝试替换mouseup和mousedown事件上的lastClick div文本,但此代码无效。有什么建议?

$('document').on(

{mousedown: function(){ $('#lastClick').html('down');}}, 

{mouseup: function(){$('#lastClick').html('up');}}

); 

HTML

<body>
<div id='lastClick'>not clicked yet!</div>
</body>

4 个答案:

答案 0 :(得分:3)

请改为尝试:

$('#lastClick').on('mousedown', function() {
    $(this).html('down');
});
$('#lastClick').on('mouseup', function() {
    $(this).html('up');
});​

请参阅此live example

答案 1 :(得分:3)

删除你的报价

$(document).on(

{mousedown: function(){ $('#lastClick').html('down');}}, 

{mouseup: function(){$('#lastClick').html('up');}}

); 

答案 2 :(得分:3)

试试这个

HTML

<body>
   <div id='lastClick'>not clicked yet!</div>
</body>

JS CODE

$(document).ready(function(){
   $('#lastClick').on(

  {mousedown: function(){ $(this).html('down');}}, 

  {mouseup: function(){$(this).html('up');}}

 ); 
});

答案 3 :(得分:2)

虽然Philippe's identified your problemdocument周围不必要的引号)我确实认为你的事情过于复杂,但可能只是为了证明你的问题。但是,在这种情况下,您尝试在代码中执行的操作可以简单地复制:

$(document).on('mousedown mouseup', function(e){
    $('#lastClick').text(e.type);
});​

JS Fiddle demo

如果您确实需要字符串为'up''down',那么您可以使用字符串操作修改上述内容:

$(document).on('mousedown mouseup', function(e){
    $('#lastClick').text(e.type.replace('mouse',''));
});​

JS Fiddle demo