[英]'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.