Javascript 使用 Google 日历提要时如何禁用 FullCalendar 中的事件链接?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7813271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:41:46  来源:igfitidea点击:

How to disable event links in FullCalendar when using Google Calendar feed?

javascriptfullcalendargoogle-calendar-api

提问by Cninroh

I am using FullCalendar library to load events in my calendar from Google Calendars. Unfortunately after events have been added to the calendar, they are clickable. When you click on the event you are automatically redirected to the Google Calendars page to view that specific event, or if you have enaught access rights - to directly edit it. While this is very useful for event management, I cannot imagine why a site visitor would like to be redirected to an external page every time he clicks on event in a calendar.

我正在使用 FullCalendar 库从 Google 日历加载日历中的事件。不幸的是,在事件被添加到日历后,它们是可点击的。当您点击事件时,您会自动重定向到 Google 日历页面以查看该特定事件,或者如果您没有访问权限 - 直接对其进行编辑。虽然这对事件管理非常有用,但我无法想象为什么站点访问者每次单击日历中的事件时都希望被重定向到外部页面。

Is there a way to disable "open on click" in the FullCalendar, overwriting link opening to an empty javascript function call could also be an option.

有没有办法在 FullCalendar 中禁用“点击打开”,也可以选择覆盖打开一个空 javascript 函数调用的链接。

采纳答案by seanb

Might be worth trying your own event renderer in the fullcalendar options:

可能值得在 fullcalendar 选项中尝试自己的事件渲染器:

{ eventRender:function (event, element)}  

To do this, you will need to write all of the rendering code yourself - can start with the original implementation and tweak as needed.
Have not tried this wih a google calendar implementation, but have used it with custom json to turn on or off href as needed.

为此,您需要自己编写所有渲染代码 - 可以从原始实现开始并根据需要进行调整。
还没有在谷歌日历实现中尝试过这个,但已经将它与自定义 json 一起使用以根据需要打开或关闭 href。

Alternatively, you could:
Hack the gcal.js file to make it not set the href property on the event objects.
Or
Intercept the event data before rendering, and remove the href property.

或者,您可以:
破解 gcal.js 文件,使其不在事件对象上设置 href 属性。
或者
在渲染前截取事件数据,去掉href属性。

回答by groper

The documentation on the FullCalendar website refers to the callback function 'eventClick':

FullCalendar 网站上的文档是指回调函数“eventClick”:

http://arshaw.com/fullcalendar/docs/mouse/eventClick/

http://arshaw.com/fullcalendar/docs/mouse/eventClick/

If the url property is set on the Event Object, then returning false prevents the browser from visiting the event url. So, when you intialise FullCalendar add the eventClick callback function with something along the lines of...

如果事件对象上设置了 url 属性,则返回 false 会阻止浏览器访问事件 url。因此,当您初始化 FullCalendar 时,请添加 eventClick 回调函数,其中包含以下内容...

$('#calendar').fullCalendar({
    eventClick: function(event) {
        if (event.url) {
            return false;
        }
    }
});

回答by good human

Edit file gcal.js

编辑文件 gcal.js

events.push({
    id: entry['gCal$uid']['value'],
    title: entry['title']['$t'],
    //url: url,
    start: start,
    end: end,
    allDay: allDay,
    location: entry['gd$where'][0]['valueString'],
    description: entry['content']['$t']

Delete line: url: url,

删除行: url: url,

回答by Silvestre Herrera

Old question, but here's how I "fixed" it.

老问题,但这是我“修复”它的方法。

eventRender(event, element) {
    element.on('click', e => e.preventDefault());
}

This will most likely prevent you from implementing other click behaviors, like opening a form to edit an event when you click it.

这很可能会阻止您实现其他点击行为,例如在您点击事件时打开一个表单来编辑它。

If you added a classNameto your Google Calendar events you might want to do it like this

如果您className在 Google 日历活动中添加了一个,您可能希望这样做

eventRender(event, element) {
    element.on('click', e => {
        if (!!element.closest('.your-gcalendar-class-name').length) {
            e.preventDefault();
        }
    });
}

That might work…

那可能有用……

回答by Fr0zenFyr

Adding a solution that works with FullCalendar v3 as of today (Aug 2018).

添加一个适用于今天(2018 年 8 月)的 FullCalendar v3 的解决方案。

To prevent eventClickfrom redirecting to the urlspecified in events, just use .preventDefault()like:

要防止eventClick重定向到中url指定的events,只需使用.preventDefault()如下:

eventClick: function( event, jsEvent, view ) {
    jsEvent.preventDefault();


    // click events previously defined will also be prevented
    // so, any alternate tasks (like showing modal) on eventClick must go here
}

回答by user3195270

This worked for me

这对我有用

eventClick: function (event) {
    // Prevent redirect to Google Calendar
    event.jsEvent.cancelBubble = true;
    event.jsEvent.preventDefault();
}

回答by user1568807

I edited the fullcalendar.fullcalendar.jsfile in sites/all/modules/fullcalendarand commented out the lines 12 to 17 like :

我编辑了fullcalendar.fullcalendar.js文件sites/all/modules/fullcalendar并注释掉了第 12 到 17 行,例如:

 var options = {
      eventClick: function (calEvent, jsEvent, view) {
      /* if (settings.sameWindow) {
          window.open(calEvent.url, '_self');
        }
        else {
          window.open(calEvent.url);
        } */
        return false;
      },