jQuery每个循环

时间:2011-09-25 07:53:02

标签: jquery loops each

目前这是静态代码:

如果您将div ID与第二个列表的hrefs的比较加上+1

例如

slide0»#pic1到#pic5

slide1»#pic2到#pic6

等等..我应该如何使用jquery每个循环或任何其他jquery方法实现这一点? 提前谢谢

<div id="my-slides">
    <div id="slide0">
        <ol>
            <li><img class="blank" src="images/thumbnails/blank.png" alt="" /></li>
            <li><a href="#pic1"><img src="images/thumbnails/pic1.jpg" alt="Winter branches" /></a></li>
            <li><a href="#pic2"><img src="images/thumbnails/pic2.jpg" alt="Wading birds" /></a></li>
            <li><a href="#pic3"><img src="images/thumbnails/pic3.jpg" alt="Bird on a post" /></a></li>
            <li><a href="#pic4"><img src="images/thumbnails/pic4.jpg" alt="Early bloomers" /></a></li>
            <li><a href="#pic5"><img src="images/thumbnails/pic5.jpg" alt="Green lizard" /></a></li>
            <li><a href="#slide1" ><b>Next</b></a></li>
        </ol>
    </div>

    <div id="slide1">
        <ol>
            <li><a href="#slide0"><b>Previous</b></a></li>
            <li><a href="#pic2"><img src="images/thumbnails/pic2.jpg" alt="Wading birds" /></a></li>
            <li><a href="#pic3"><img src="images/thumbnails/pic3.jpg" alt="Bird on a post" /></a></li>
            <li><a href="#pic4"><img src="images/thumbnails/pic4.jpg" alt="Early bloomers" /></a></li>
            <li><a href="#pic5"><img src="images/thumbnails/pic5.jpg" alt="Green lizard" /></a></li>
            <li><a href="#pic6"><img src="images/thumbnails/pic6.jpg" alt="Feeding the birds" /></a></li>
            <li><a href="#slide1" ><b>Next</b></a></li>
        </ol>
    </div>

    <div id="slide2">
    <ol>
            <li><a href="#slide1"><b>Previous</b></a></li>
            <li><a href="#pic7"><img src="images/thumbnails/pic7.jpg" alt="Feeding the birds" /></a></li>
            <li><a href="#pic8"><img src="images/thumbnails/pic8.jpg" alt="A group of butterflies" /></a></li>
            <li><a href="#pic9"><img src="images/thumbnails/pic9.jpg" alt="Ladybirds" /></a></li>
            <li><a href="#pic10"><img src="images/thumbnails/pic10.jpg" alt="Butterfly" /></a></li>
            <li><a href="#pic11"><img src="images/thumbnails/pic11.jpg" alt="Trees in the mist" /></a></li>
            <li><img class="blank" src="images/thumbnails/downx.png" alt="" /></li>
    </ol>
    </div>

3 个答案:

答案 0 :(得分:2)

这是你要找的吗?

$('a[href^=#pic]').each(function() {
    alert($(this).attr('href').match(/^#pic(\d+)/)[1]);
});

答案 1 :(得分:0)

$("ol li a:not(:first-child),ol li a:not(:last-child)").each(function()
{
    $(this).attr("href",<value>);
})

答案 2 :(得分:0)

试试这个:

$('#my-slides a img').each(function() {
    var pic_number = $(this).parent().attr('href').substr(3);
    $(this).parent().parent().attr('href', pic_number);
}