javascript - 根据条件允许/阻止默认事件

标签 javascript jquery events jquery-events

我们正在尝试实现以下要求:用户单击链接,我们现在需要调用一个函数,然后(假设 2000 毫秒后)执行默认链接事件(可以是打开下一页、mailto 链接)或其他)。

我们尝试了很多与此方法类似的方法:

HTML

<a class="link-elem" href="foo.html">Foo Bar</a>

事件监听器

$('.link-elem').on('click', function(event, fromFunction) {
    if (fromFunction) {
      // do the default event
      console.log('event triggered again with fromFunction = true');
    } else {
      // call function
      otherFunction(event, {foo: 'bar'});

      // prevent default behavior
      // (tried all combinations of these)
      // return false;
      event.preventDefault();
      event.stopPropagation();
      event.stopImmediatePropagation();
    }
});

其他功能

var otherFunction = function(event, otherArg) {
   // do function stuff

   setTimeout(function() {
     // trigger event again with fromFunction = true
     $(event.target).trigger(event.type, true);
   }, 2000);
};

JS在这里摆弄:https://jsfiddle.net/kwh5tay8/

这样做的结果是我们看到了 console.log 消息,但实际事件没有发生,即下一页未加载。

我们无法读取 href 属性并执行诸如 window.location 之类的操作,因为我们不能依赖该事件作为简单链接。

有什么办法可以解决这个问题吗?

最佳答案

我猜你的问题是,你调度了事件但你没有执行任何东西, 我准备了一个示例 js 文件,您可以这样使用。

                var operation = "";

                $('.link-elem').on('click', function(event, fromFunction) {
                    if (fromFunction) {
                      // do the default event
                      callRequiredFunc();
                    } else {
                      // call function
                      otherFunction(event, { type : 'link', link: 'http://google.com'});

                      // prevent default behavior
                      // (tried all combinations of these)
                      // return false;
                      event.preventDefault();
                      event.stopPropagation();
                      event.stopImmediatePropagation();
                    }
                });

                var otherFunction = function(event, otherArg) {
                   // do function stuff
                 operation = otherArg
                   setTimeout(function() {
                     // trigger event again with fromFunction = true
                     $(event.target).trigger(event.type, true);
                   }, 2000);
                };

                var callRequiredFunc = function(event){
                        switch(operation.type){
                            case "mail":
                                $(location).attr('href', 'mailto:?subject='
                             + encodeURIComponent("This is my subject")
                             + "&body=" 
                             + encodeURIComponent("This is my body")
                                 );
                            break;
                            case "link":
                                window.location.href = operation.link
                            break;
                            case "whatever":
                            break;
                            default:
                            break;
                        }
                } 

关于javascript - 根据条件允许/阻止默认事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30305232/

相关文章:

javascript - 使用 jquery 进行多选验证

选择元素选择的 Javascript 事件

Javascript 新对象字段未保存

javascript - 等高不工作.js 文件

javascript - JS 如何从each循环内的数组内的数组中获取对象值

javascript - 如何通过触发函数的元素的父div的类名获取另一个子元素

java - 在Java中,我可以在每次事件分派(dispatch)线程从处理输入事件返回时调用一个例程吗?

events - 功能性过剩?

php - 比较 http_referer 值

javascript - 表单验证错误 : Anytime I click on the submit button, 我在 errorDiv 上收到附加错误结果