0

我制作了一个包含点击事件和函数的插件:

// my function
jQuery.fn.runSlideshow = function() {

  // in here I could write:
  jQuery(this).runSlideshow();
  // to call itself again
});

jQuery(this).find(".navigation-wrap a").click(function(ev){
  ev.preventDefault();
  // some code


  // in here, jQuery(this) points to the <a>-tag I clicked on
  alert(jQuery(this).html());


  // but now I want to call the function..
  jQuery(this).runSlideshow(); // won't work
  jQuery.fn.runSlideshow();  // won't work either

})

我知道它现在无法工作,因为在单击事件中,我被困在引用我单击的元素并且无法返回“功能根”。知道该怎么做吗?

4

1 回答 1

0

这对我来说很好:查看我的 jsfiddle

// my function
jQuery.fn.runSlideshow = function(i) {
  console.log('in runSlideshow i = ' + i);

  // in here I could write:
  if (i > 0) jQuery(this).runSlideshow(i-1);
  // to call itself again
};

$(".navigation-wrap a").click(function(ev){
    ev.preventDefault();
  // some code

  // in here, jQuery(this) points to the <a>-tag I clicked on
  alert(jQuery(this).html());

  // but now I want to call the function..
    jQuery(this).runSlideshow(3); //  works
    console.log('call again');
    jQuery.fn.runSlideshow(2);  // works twoo

});
于 2013-07-06T01:42:19.063 回答