JS插入DOM元素后如何让jcarousellite绑定?

发布于 2024-10-09 18:22:18 字数 480 浏览 0 评论 0原文

我在我的一个项目中使用 jcarousellite。这是我到目前为止的代码。

$(".carousel").jCarouselLite({
    btnNext: ".next",
    btnPrev: ".prev",
    speed: 700,
    visible: 8,
    afterEnd: function(a){
        // set the now first element to the active video
        $(a[0]).addClass("active");
    },
});

我的列表项才生成

$(document).ready(function(){
  // generate list items
});

唯一的问题是,直到我想在加载列表项后生成轮播时, 。我可以为此使用 jQuery 的 .live() 吗?有什么想法吗?

I am using jcarousellite on one of my projects. This is the code I have so far.

$(".carousel").jCarouselLite({
    btnNext: ".next",
    btnPrev: ".prev",
    speed: 700,
    visible: 8,
    afterEnd: function(a){
        // set the now first element to the active video
        $(a[0]).addClass("active");
    },
});

Only problem is that my list items have not been generated until

$(document).ready(function(){
  // generate list items
});

I would like to generate my carousel after the list items have been loaded. Can I use jQuery's .live() for this? Any ideas?

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

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

发布评论

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

评论(1

§普罗旺斯的薰衣草 2024-10-16 18:22:18

在生成项目之前不要运行轮播插件。

$(document).ready(function(){

  // generate list items

  // then run the jCarouselLite
  $(".carousel").jCarouselLite({
    btnNext: ".next",
    btnPrev: ".prev",
    speed: 700,
    visible: 8,
    afterEnd: function(a){
        // set the now first element to the active video
        $(a[0]).addClass("active");
    },
  });

});

如果列表项是由于异步 AJAX 调用而生成的,则将轮播代码放入 AJAX 调用的回调中。

如果您要动态生成其他项目,则将对列表的引用存储在变量中,并仅针对这些项目调用轮播插件。

不,您不能为此使用 .live()

Don't run the carousel plugin until after your items have been generated.

$(document).ready(function(){

  // generate list items

  // then run the jCarouselLite
  $(".carousel").jCarouselLite({
    btnNext: ".next",
    btnPrev: ".prev",
    speed: 700,
    visible: 8,
    afterEnd: function(a){
        // set the now first element to the active video
        $(a[0]).addClass("active");
    },
  });

});

If the list items are being generated as a result of an asynchronous AJAX call, then put the carousel code in the callback to the AJAX call.

If you're generating additional items dynamically, then store a reference to the list in a variable, and call the carousel plugin against just those items.

And no, you can't use .live() for this.

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