jquery bind change css

2019-07-09 01:55发布

问题:

I'm trying the following:

$('table.table_record_even').bind("contextmenu", function(e) {
    $('#right_menu').css({
        top: e.pageY+'px',
        left: e.pageX+'px'
    }).show();
    return false;

    $(this).css({"background":"#FFFBCC"});

});

When rightclick on the table the css doesn't change. Someone knows how to solve it?

Thnx

EDIT

I've changed the code a little bit and i've made a fiddle to explain a little better. As you can see, with right clicking there is a little problem with remove the hover class.

http://fiddle.jshell.net/9Ku7h/5/

回答1:

You were returning false before processing the css;

    $('table.table_record_even').bind("contextmenu", function(e) {
        $('#right_menu').css({
            top: e.pageY+'px',
            left: e.pageX+'px'
        }).show();


        $(this).css({"background":"#FFFBCC"});
    return false;

    });


回答2:

$('table.table_record_even').mousedown(function(e) {
    if (e.which === 3) {
        /* Right Mousebutton was clicked! */
        $('#right_menu').css({
           top: e.pageY+'px',
           left: e.pageX+'px'
        }).show();

        $(this).css({"background":"#FFFBCC"});
        return false;
    }
});


回答3:

This is because return statement, just move it to the bottom of your function:

$('table.table_record_even').bind("contextmenu", function(e) {
    $('#right_menu').css({
        top: e.pageY+'px',
        left: e.pageX+'px'
    }).show();
    $(this).css({"background":"#FFFBCC"});
    return false;

});


回答4:

try:

$('table.table_record_even').bind("contextmenu", function(e) {
    $('#right_menu').css({'position':'absolute',
        "top": e.pageY+'px',
        "left": e.pageX+'px'
    }).show();


    $(this).css({"background":"#FFFBCC"});
    return false;

});


标签: jquery bind