jquery - jQuery绑定(bind)事件上的RequireJS不会触发方法

标签 jquery events error-handling binding requirejs

将 requirejs 与 jquery 一起使用,我遇到了调用绑定(bind)事件的方法的问题。具有以下代码:

define(["jquery"], function($) {


  function showRowItem(item) {
        console.log('method in');
       
  }
  
  jQuery(document).ready(function () {

        jQuery('ul#topnav-firstrow li').each(function () {

            jQuery(this).bind("mouseover", function (event) {
                if (outTimerID != null) {
                    clearTimeout(outTimerID);
                    outTimerID = null;
                }
                globalMouseOverItem = this;
                inTimerID = window.setTimeout("showRowItem(globalMouseOverItem)", inDelay);
            });
          
          });
    });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>


当我将鼠标悬停在项目上时,事件被触发但方法无法识别
Uncaught ReferenceError: showRowItem is not defined

这是一个密码笔http://codepen.io/deroccha/pen/WxQLoy用于重现错误

最佳答案

将字符串传递给 setTimeout既不好也没有必要,这样做会导致您遇到问题,因为字符串的评估被延迟,因此发生在 showRowItem 的上下文之外。可用。你可以这样做:

window.setTimeout(showRowItem.bind(globalMouseOverItem), inDelay);

或者使用您拥有的当前代码,这将是等效的:
window.setTimeout(showRowItem.bind(this), inDelay);

你可以省略 globalMouseOverItem .

关于jquery - jQuery绑定(bind)事件上的RequireJS不会触发方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37697916/

相关文章:

javascript - 下拉菜单,选择一项时调用一个函数,选择另一项时调用另一个函数

javascript - 主干 - 在模型更改时注销 View 事件

jQuery:从类中获取可见元素的值

jquery - 如何拆分输入值并插入到 tspan 中

javascript - 在 lightbox2 加载时旋转图像

events - Magento 添加新产品事件观察者

vb.net - 使用子例程和函数进行错误处理的正确方法是什么?

python - DOCPLEX:添加一个对两个变量求和的约束

c# - ASP.NET MVC 4 : error when making Ajax call to controller to update SQL Server database

javascript - Bootstrap 将事件更改为禁用