click event in jQuery and right mouse clicking

一曲冷凌霜 提交于 2020-01-22 14:32:09

问题


Does click trigger when right mouse button was clicked? I want to implement a right click kind of menu with full calendar, but it only has dayClick event, which I think is triggered only when left mouse button is clicked. I was thinking something like

dayClick: function(date, allDay, jsEvent){
    if (jsEvent.button === 1){
         //show menu
    }else{
        //do something with day
    }
}

but dayClick isn't triggered when right mouse is clicked....Any other ideas?


回答1:


Try binding mousedown to each FullCalndar event in your eventRender event:

var events_array = [{
    title: 'Test1',
    start: new Date(2013, 11, 20)
}, {
    title: 'Test2',
    start: new Date(2013, 11, 21)
}];

$('#mycalendar:not(".fc-event")').on('contextmenu', function (e) {
    e.preventDefault()
})

$('#mycalendar').fullCalendar({
    events: events_array,
    header: {
        left: 'prevYear,prev,next,nextYear today',
        center: 'title',
        right: 'month,agendaWeek,agendaDay'
    },
    eventRender: function (event, element) {
        element.bind('mousedown', function (e) {
            if (e.which == 3) {
                alert('Right mouse button pressed');
            }
        });
    }
});

You can disable right click on page and let it act only on events using:

$('#mycalendar:not(".fc-event")').on('contextmenu', function(e){ e.preventDefault() })

Demo: http://jsfiddle.net/IrvinDominin/3bukS/




回答2:


Your answer is No, click doesn't trigger when right mouse button is clicked, but you can try mousedown event, check this out:

jQuery(document.body).on("mousedown", function(event){
    if(event.button==2){
        //do what you want
    }
});



回答3:


in my case, i do disable the right click context menu first:

$("#calendar").on("contextmenu",function (event) {
              event.preventDefault();
              });

then on the render event i add this:

  eventRender: function(event, element) {
...
 var event2=event;
     element.on('contextmenu', function (event) { showMenu(event2.start,event2,event.pageX,event.pageY); });
...
}

where ShowMenu is my own menu Div with options, in the same cursor position...




回答4:


Here is the my solution for handling right click event of full calendar.Edit dayMousedown function as follows on fullcalendar.js

dayMousedown: function(ev) {
        var view = this.view;

        // HACK
        // This will still work even though bindDayHandler doesn't use GlobalEmitter.
        if (GlobalEmitter.get().shouldIgnoreMouse()) {
            return;
        }
        switch (ev.which) {
            case 1:
                this.dayClickListener.startInteraction(ev);
                break;
            case 2:
                alert('Middle Mouse button pressed.');
                break;
            case 3:
                // here we can handle right click functionality 
                this.dayRightClickListener.startInteraction(ev);

                break;
            default:
                alert('You have a strange Mouse!');
        }

        if (view.opt('selectable')) {
            this.daySelectListener.startInteraction(ev, {
                distance: view.opt('selectMinDistance')
            });
        }
    }

And I implemented dayRightClickListener function to handle my requirement.



来源:https://stackoverflow.com/questions/20610774/click-event-in-jquery-and-right-mouse-clicking

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!