将 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/