简体   繁体   English

使用jQuery触发链接单击时的“未定义”功能

[英]'undefined' function when triggering a click of a link with jQuery

For some reason this works in FF 11, Chrome, and even in IE but not in FF 3.6 or Safari 5. 由于某种原因,它可以在FF 11,Chrome和IE中使用,但不能在FF 3.6或Safari 5中使用。

Basically I'm triggering the click of a link with: 基本上,我是通过以下方式触发链接的点击:

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);
}​

But when I click on the link in FF 3.6 or Safari 5 I get the following error: 但是,当我在FF 3.6或Safari 5中单击链接时,出现以下错误:

TypeError: 'undefined' is not a function (evaluating '$nextItem.click()')

Is there any particular "gotcha" that I don't know about here using the following method? 使用以下方法,在这里我是否不知道任何特定的“陷阱”?

Try .eq() so $nextItem is a jQuery collection: 试试.eq()这样$nextItem是一个jQuery集合:

var $nextItem = modalSeriesList.eq(index + 1);

// ...

$nextItem.click()

Using [...] retrieves the DOM Object, which doesn't have a .click() method. 使用[...]检索没有.click()方法的DOM对象。

You could try 你可以试试

if( typeof $nextItem == 'undefined' ){
    $nextButton.trigger('click');
}

instead of 代替

if ($nextItem) {
    $nextButton.click(nextClick);
}​

if you turn condition of if statement into 如果将if语句的条件转换为

if(typeof $nextItem == 'undefined' )

it works. 有用。 As Porco said probably $nextItem does not exist.... 如Porco所说,$ nextItem可能不存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM