尝试调用函数“clickEvent()”并使用jQuery将其传递给带有arrow-keyUp属性的obj。但我没有达到这个功能。这是代码。感谢。
请注意:我正在分配现有代码,但是将arrowPress事件添加到其中。因此,如果(direction!= null)区域,所有内容都应保留为文档准备就绪。
$(document).ready(function() {
$('body').keyup(function(event) {
var direction = null;
// handle cursor keys
if (event.keyCode == 37) {
// slide left
direction = 'prev';
} else if (event.keyCode == 39) {
// slide right
direction = 'next';
}
if (direction != null) {
//alert($('.'+ direction).attr('rel'));
//need to pass the alert above as an obj to the function clickEvent() below.
}
});
}); //end $(document).ready
//function on external .js src
(function($) {
function clickEvent(obj) {
alert(obj.attr("rel"));
}
$.fn.slidingPage = function(options) {
$el = $(this);
}
})(jQuery); //end ext js scr
答案 0 :(得分:0)
函数clickEvent
位于另一个函数的范围内,将它们放在同一范围或更高范围内:
function clickEvent(obj) {
alert(obj.attr("rel"));
}
$.fn.slidingPage = function(options) {
var $el = $(this);
};
(function($) {
$('body').keyup(function(event) {
var direction = null;
// handle cursor keys
if (event.keyCode == 37) {
// slide left
direction = 'prev';
} else if (event.keyCode == 39) {
// slide right
direction = 'next';
}
if (direction != null) {
clickEvent( $('.'+ direction) );
}
});
});
clickEvent
和slidingPage
声明没有理由在文档就绪处理程序中。
答案 1 :(得分:0)
如果您需要将代码包装到匿名函数中以防止全局变量污染,那么您的事件和函数代码应该放在相同的范围内。
您可能需要在.prev和.next上触发点击事件。
$(document).ready(function() {
$('body').keyup(function(event) {
var direction = null;
// handle cursor keys
if (event.keyCode == 37) {
// slide left
direction = 'prev';
} else if (event.keyCode == 39) {
// slide right
direction = 'next';
}
if (direction != null) {
$('.'+ direction).click()
}
});
}); //end $(document).ready
答案 2 :(得分:0)
感谢Andrey Kuzmin ......
if (direction != null) {
$('.'+ direction).click();
}