jQuery是鼠标悬停的mousedown [英] jQuery is mousedown on mouseover

查看:110
本文介绍了jQuery是鼠标悬停的mousedown的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个桌子,我想要改变鼠标的鼠标和鼠标按钮,我目前的解决方案不能正常工作,因为我想要:

  function ChangeColor(sender){

sender.style.backgroundColor ='yellow';

}

var click = false;


$(document).mouseup(function(){
click = false;
});


$(document).ready(function(){
$('#Table1 tr')。each(function(){
$('td ',this).each(function(){
$(this).mousedown(function(){
click = true;
});
$(this) mousedown(function(event){
if(click == true){ChangeColor(this);}
});
});
});
});

有没有办法让它这样工作?

解决方案

编辑:鉴于您的评论,您可以这样做:

  $(document).ready(function(){
isMouseDown = false

$('body')。mousedown(function(){
isMouseDown = true;
})
.mouseup(function(){
isMouseDown = false;
});

$('表1 tr td')。mouseenter(function(){
if(isMouseDown)
$(this).css({backgroundColor:'orange'});
});
});

这将使 td的背景颜色当你鼠标悬停,但只有当鼠标按钮关闭时。






听起来就像你只是想改变颜色,当你单击。如果是这样的话,这比你尝试的要简单得多。

  $(document).ready(){

$('#Table1 tr td')。click(function(){
$(this).css({backgroundColor:'yellow'});
});

});

这将改变 td的背景当您点击它们时,元素为黄色。



当您滑鼠移动时,它将类似于更改颜色。



编辑:刚刚注意到您的问题的标题。



如果您希望在您悬停时触发点击...

  $(document).ready(){

$('#Table1 tr td')。 {
$(this).css({backgroundColor:'yellow'});
})
.mouseenter(function(){
$(this).click() ;
});

});

...当然可以消除点击在这种情况下,只需更改 mouseenter 事件的背景。


I have a table where i want to change cell background on mouse over and mouse button down, my current solution doesn't work as I want it to :

function ChangeColor(sender) {

    sender.style.backgroundColor = 'yellow';

}

var clicking = false;


$(document).mouseup(function() {
    clicking = false;
});


$(document).ready(function() {
    $('#Table1 tr').each(function() {
        $('td', this).each(function() {
            $(this).mousedown(function() {
                clicking = true;
            });
            $(this).mousedown(function(event) {
                if (clicking==true) { ChangeColor(this); }
            });
        });
    });
});

Is there any way to make it work like that ?

解决方案

EDIT: Given your comment above, you could do something like this:

$(document).ready(function() {
    isMouseDown = false

    $('body').mousedown(function() {
        isMouseDown = true;
    })
    .mouseup(function() {
        isMouseDown = false;
    });

    $('Table1 tr td').mouseenter(function() {
        if(isMouseDown)
            $(this).css({backgroundColor:'orange'});
    });
});

This will color the background of the td when you mouseover, but only if the mouse button is down.


Sounds like you just want to change the color when you click. If that's the case, it is much simpler than what you're attempting.

$(document).ready() {

    $('#Table1 tr td').click(function() {
        $(this).css({backgroundColor:'yellow'});
    });

});

This will change the background of the td elements yellow when you click them.

It will be similar to change the color when you mouseover.

EDIT: Just noticed the title of your question.

If you want to trigger a click when you hover...

$(document).ready() {

    $('#Table1 tr td').click(function() {
        $(this).css({backgroundColor:'yellow'});
    })
     .mouseenter(function() {
         $(this).click();
     });

});

...of course, you could eliminate the click in that case and just change the background with the mouseenter event.

这篇关于jQuery是鼠标悬停的mousedown的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆