最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery - Programmatically Trigger Event - Stack Overflow

programmeradmin0浏览0评论

I need to programmatically trigger a click event that's being handled by jQuery. Here's the current code:

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

I am having a mental block working out how to create a function out of the event handling code and then arbitrarily calling it for an element in the $thumbs object.

I need to programmatically trigger a click event that's being handled by jQuery. Here's the current code:

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

I am having a mental block working out how to create a function out of the event handling code and then arbitrarily calling it for an element in the $thumbs object.

Share Improve this question asked Apr 23, 2010 at 16:25 SonnySonny 8,3367 gold badges65 silver badges137 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5
$thumbs.click();

This would trigger the click event. Is this what you're looking for?

Similar to the previous suggestion of

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

you can use

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

For clarification, array indexing into a jQuery collection gives you the DOM Element at that position, whereas .eq(n) gives you a new jQuery object which references only the indexed element.

http://api.jquery./eq/

发布评论

评论列表(0)

  1. 暂无评论