当一个对象被赋予一个类时,jQuery 触发事件

发布于 2024-10-01 19:03:46 字数 573 浏览 1 评论 0 原文

我正在使用 jQuery 轮播,从这里下载: http://www.thomaslanciaux.pro/jquery/ jquery_carousel.htm

它使用分页,并在单击页码时将一个名为“active”的类应用于当前页码。我想在应用此类时触发一个事件,因为它还使用左右箭头将该类应用于当前页码。

这是我希望在应用类时触发的事件:

$('.red').click(function() {
     $('#formred').attr('checked', 'checked');
 });

我已经让它工作,但仅在页面加载时:

 if ($('.red').hasClass('active')) {
      $('.red').trigger('click');
 }

第一个页码默认应用了该类,这就是为什么它在页面加载时起作用。当该类应用于其他页码时,我需要发生这种情况。

I am using a jQuery carousel, downloaded from here: http://www.thomaslanciaux.pro/jquery/jquery_carousel.htm

It uses pagination, and applies a class called "active" to the current page number when the page number is clicked. I want to trigger an event to happen when this class is applied, since it also uses left and right arrows to apply the class to the current page number.

Here's the event that I want to be triggered when the class is applied:

$('.red').click(function() {
     $('#formred').attr('checked', 'checked');
 });

I have gotten it to work, but only when the page loads:

 if ($('.red').hasClass('active')) {
      $('.red').trigger('click');
 }

The first page number has the class applied by default, which is why this works when the page loads. I need this to happen as the class is applied to other page numbers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

箹锭⒈辈孓 2024-10-08 19:03:46

编辑:如果您只是在单击按钮时需要它,而不是其 auto 功能,则只需放置一个 .delegate()一旦可用,就可以在 .carousel 上侦听 'a[role=button]' 元素上的 click 事件。

$('.carousel').delegate('a[role=button]', 'click', function() {

     // Because a button in the carousel was clicked, we know that
     //   one of the elements received the .active class. So we just
     //   need to find it.
     var $activeButton = $(this).closest('.carousel').find('.active');

     // Not sure how the .red class from your question factors in, but you
     //   can test the $activeButton to see if it has that class
     if( $activeButton.hasClass('red') ) {
         $('#formred').attr('checked', 'checked');
     }
});

同样,这仅适用于按钮上的单击事件,如果设置为自动旋转则无效。不确定这是否是您所追求的。


如果需要,有一个名为 livequery 的插件,它将通过 jQuery 在 DOM 修改上运行代码。

http://brandonaaron.net/code/livequery/docs

$('.someClass').livequery( function() {
    // your code
});

如果传递第二个函数参数,当类(或元素)被删除时它将运行。

$('.someClass').livequery( function() {
    // your code when adding
}, function() {
    // your code when removing
});

EDIT: If you're just needing it on button clicks, and not for its auto feature, you could just place a .delegate() on the .carousel once it's available that listens for click events on the 'a[role=button]' elements.

$('.carousel').delegate('a[role=button]', 'click', function() {

     // Because a button in the carousel was clicked, we know that
     //   one of the elements received the .active class. So we just
     //   need to find it.
     var $activeButton = $(this).closest('.carousel').find('.active');

     // Not sure how the .red class from your question factors in, but you
     //   can test the $activeButton to see if it has that class
     if( $activeButton.hasClass('red') ) {
         $('#formred').attr('checked', 'checked');
     }
});

Again, this would only work for click events on the buttons, not if it is set up to auto rotate. Not sure if this is what you're after.


If need be, there's a plugin called livequery that will run code on DOM modifications via jQuery.

http://brandonaaron.net/code/livequery/docs

$('.someClass').livequery( function() {
    // your code
});

If you pass a second function argument, it will run when the class (or element) is removed.

$('.someClass').livequery( function() {
    // your code when adding
}, function() {
    // your code when removing
});
真心难拥有 2024-10-08 19:03:46

您不能创建一个共享事件处理程序,例如:

function changePage( pageNum, nextprev ){
  if( nextprev ) pageNum = activePage + nextprev;

  // show the next page

  $('someElement').addClass('active');
}

然后将此事件设置为单击和按键:

$(...).keydown( function(){
  changePage( [1 or -1, depending on input], true );
});

$(...).click( function(){
  changePage( [clicked page], false );
});

Couldn't you create a shared event handler, like:

function changePage( pageNum, nextprev ){
  if( nextprev ) pageNum = activePage + nextprev;

  // show the next page

  $('someElement').addClass('active');
}

And then set this event to both click and keydown:

$(...).keydown( function(){
  changePage( [1 or -1, depending on input], true );
});

$(...).click( function(){
  changePage( [clicked page], false );
});
毁梦 2024-10-08 19:03:46

我要做的是:

1-禁用自动播放

2-创建一个setInterval,在轮播的分页上执行触发(“单击”)

3-当然,在其中您可以做更多的事情,而不仅仅是触发幻灯片

What I would do is :

1-disable autoplay

2-create a setInterval that does a trigger('click') on the pagination of the carousel

3-and of course inside that you could do more then just trigger the slides

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文