简体   繁体   English

在jQuery事件处理程序中调用自定义函数

[英]Calling a custom function in jQuery event handler

This is the function I want to call (it definitely works I tested it): 这是我要调用的函数(经过测试,它肯定可以正常工作):

function enableCategoryPopup(event) {
    event.stopPropagation();
    var buttonOffset = $(this).offset();
    $('.category-popup').css({
      top: buttonOffset.top + 10,
      left: buttonOffset.left +10
    });
    $('.category-popup').show(100);
  }

And this is where I want to call it: 这是我要称呼它的地方:

$(document).on("mousedown", ".tab-links li:nth-child(2)", function(event) {
    if (event.which == 3) {
      enableCategoryPopup();
    }
  });

My custom function should be called as soon as I right click the specified element but it doesn't. 右键单击指定的元素,但不会立即调用我的自定义函数。 When I call alert("x") instead of enableCategoryPopup() it works, only my custom function doesn't get executed. 当我调用alert("x")而不是enableCategoryPopup()它会起作用,只有我的自定义函数不会执行。

What am I doing wrong? 我究竟做错了什么? How do I call my custom function? 如何调用自定义函数?

try handling the event from within the enableCategoryPopup function 尝试从enableCategoryPopup函数中处理事件

function enableCategoryPopup(event) {
    if (event.which == 3) {
        event.stopPropagation();
        var buttonOffset = $(this).offset();
        $('.category-popup').css({
          top: buttonOffset.top + 10,
          left: buttonOffset.left +10
        });
        $('.category-popup').show(100);
    }
  }

  $(document).on("mousedown", ".tab-links li:nth-child(2)", enableCategoryPopup);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM