[英]'undefined' function when triggering a click of a link with jQuery
由于某种原因,它可以在FF 11,Chrome和IE中使用,但不能在FF 3.6或Safari 5中使用。
基本上,我是通过以下方式触发链接的点击:
var $currentItem = $('#' + currentId);
var modalSeriesList = $('.js-modal-series');
var index = modalSeriesList.index($currentItem);
var $nextItem = modalSeriesList[index + 1];
var nextClick = function() {
$(document).off('keydown.general');
$nextItem.click();
}
$(document).off('keydown.general');
if ($nextItem) {
$nextButton.click(nextClick);
}
但是,当我在FF 3.6或Safari 5中单击链接时,出现以下错误:
TypeError: 'undefined' is not a function (evaluating '$nextItem.click()')
使用以下方法,在这里我是否不知道任何特定的“陷阱”?
试试.eq()
这样$nextItem
是一个jQuery
集合:
var $nextItem = modalSeriesList.eq(index + 1);
// ...
$nextItem.click()
使用[...]
检索没有.click()
方法的DOM对象。
你可以试试
if( typeof $nextItem == 'undefined' ){
$nextButton.trigger('click');
}
代替
if ($nextItem) {
$nextButton.click(nextClick);
}
如果将if语句的条件转换为
if(typeof $nextItem == 'undefined' )
有用。 如Porco所说,$ nextItem可能不存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.