代码之家  ›  专栏  ›  技术社区  ›  Sonny

jquery-以编程方式触发事件

  •  3
  • Sonny  · 技术社区  · 14 年前

    我需要以编程方式触发jquery正在处理的click事件。以下是当前代码:

    var $thumbs = $('#PhotoGalleryThumbs .tile');
    var $zoom = $('#PhotoGallery #PhotoGalleryZoom img');
    var $description = $('#PhotoGallery #PhotoGalleryDescription');
    
    $thumbs.click(function(event) {
        event.preventDefault();
        var $thumb = $(this);
        $thumb.addClass('selected')
            .siblings().removeClass('selected');
        $zoom.attr('src', $thumb.children('a').attr('href'));
        $description.html($thumb.find('img').attr('alt'));
    });
    

    我有一个智力模块,它在计算如何用事件处理代码创建函数,然后为中的元素任意调用函数。 $thumbs 对象。

    2 回复  |  直到 14 年前
        1
  •  5
  •   Evgeny Shadchnev    14 年前
    $thumbs.click();
    

    这将触发单击事件。这就是你要找的吗?

        2
  •  2
  •   Geoff    14 年前

    类似于之前的建议

    $($thumbs[7]).click();
    

    你可以使用

    $thumbs.eq(7).click();
    

    为了进行澄清,在jquery集合中的数组索引提供了该位置的dom元素,而 .eq(n) 给您一个新的jquery对象,它只引用索引元素。

    http://api.jquery.com/eq/