Using jQuery's .each() function to attach a function to multiple slideshow containers

a 夏天 提交于 2019-12-31 05:37:09

问题


I have very many small jQuery Cycle slideshow divs (containers) on a one-page website like

    <div class="foo bar" data-value="1000"> // data-value varies on each container
        <img src="directory/img_0.jpg" alt="img 0" />
        <img src="directory/img_1.jpg" alt="img 1" />
        <img src="directory/img_2.jpg" alt="img 2" />
    </div>

and want to cycle them all - with each slideshow div having a different data-value - without hard coding/repeating

$(document).ready(function() {
  $('.foo.bar').cycle({
      speed: 300,
      timeout: 6000,
      delay: $('.foo.bar').data('value')
  });
});

for all occurences of such slideshow div. How can I "attach" or "bind" or "link" such jQuery function to each slideshow div so that each element's differing data-value is used? I suspect that jQuery's .each() function could allow me to do so - but how?

EDIT fiddle

Thank you very much in advance!


回答1:


As you mention, .each() will let you do this quite easily:

$('.foo.bar').each(function() {
    $(this).cycle({
      speed: 300,
      timeout: 6000,
      delay: $(this).data('value')
    });
});

Inside the .each callback, this refers to the element you are currently operating on.



来源:https://stackoverflow.com/questions/11341242/using-jquerys-each-function-to-attach-a-function-to-multiple-slideshow-conta

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!