【问题标题】:How do I make a loop in jQuery so that theres a number added to the end of my divs?如何在 jQuery 中创建一个循环,以便在我的 div 末尾添加一个数字?
【发布时间】:2026-02-03 09:15:03
【问题描述】:

我希望 div id 和 div 类中的数字循环并从 0 到我拥有的任何 div .. 我怎样才能做到这一点?我试图把它放在一个 for 循环中,但由于某种原因它不起作用..

$("#bokautbildning0").hide();
$(".boka_btn0").click(function(){
        $("#bokautbildning0").slideToggle();
        var scrollToId = $(this).attr('href');
        $('html, body').animate({
            scrollTop: $(scrollToId).offset().top - 270
        }, 500);
        return false;
    });


$("#bokautbildning1").hide();
$(".boka_btn1").click(function(){
        $("#bokautbildning1").slideToggle();
        var scrollToId = $(this).attr('href');
        $('html, body').animate({
            scrollTop: $(scrollToId).offset().top - 270
        }, 500);
        return false;
    });


$("#bokautbildning2").hide();
$(".boka_btn2").click(function(){
        $("#bokautbildning2").slideToggle();
        var scrollToId = $(this).attr('href');
        $('html, body').animate({
            scrollTop: $(scrollToId).offset().top - 270
        }, 500);
        return false;
    });

这是我尝试过的循环代码:

for ( var i = 0; i < 5; i++ ) {
    $("#bokautbildning" + i).hide();
    $(".boka_btn" + i).click(function(){
        $("#bokautbildning" + i).slideToggle();
        var scrollToId = $(this).attr('href');
        $('html, body').animate({
            scrollTop: $(scrollToId).offset().top - 270
        }, 500);
        return false;
    });
  }

【问题讨论】:

  • 你的问题不是很清楚。你想完成什么,循环在哪里?
  • 你为什么不给他们所有相同的班级,而不是给班级编号?如果类对于每个 DIV 都是唯一的,则它们不是很有用。
  • 你说的是哪个数字? .boka_btn 之后的那个还是#bokautbildning1 之后的那个?
  • 因为在收到第一个答案后改变了问题的本质而被否决。
  • 当第一个答案出现时,OP 意识到他没有准确地说明他的用例。这不是对他投反对票的好理由。

标签: javascript jquery css loops


【解决方案1】:

此答案是针对旧版问题编写的,不再正确。

最好的解决方案是为所有这些元素添加一个类,并使用它而不是 id 来运行 jQuery。让 jQuery 完成所有这些元素的迭代工作。

例如,将所有元素 #bokautbildningn 赋予 .bokautbildning 类并将您的 javascript 代码更改为:

$('.bokautbildning').hide();

$('.boka_btn1').click(function(){
    $('.bokautbildning').slideToggle();

    var scrollToId = $(this).attr('href');
    $('html, body').animate({
        scrollTop: $(scrollToId).offset().top - 270
    }, 500);

    return false;
});

【讨论】:

  • 更新了我的问题,非常感谢任何帮助
【解决方案2】:

如果您使用数据属性,那么 jQuery 将非常简单。也就是说:

HTML 示例:

<div data-number="1"></div>
<div data-number="2"></div>
<div data-number="3"></div>
<div data-number="4"></div>
<div data-number="5"></div>

jquery:

$("div[data-number]").each(function() {
    var number = $(this).data("number");
    $(this).html(number);
    alert(number);
})

查看实际效果:

http://jsfiddle.net/uubxj55r/

【讨论】:

  • 这是一个非常冗长的方法。您可以只使用每个循环中的索引。不需要数据属性。
  • 因此,为页面上的每个 div 创建 1 个类的反模式更加冗长,然后使用数据属性存储数字并查询 div 是否有数据-number 属性...?
  • 谢谢!!数据属性和每个函数是一个更好的解决方案!
  • @CBauer 一点也不。这种方法比OP的原始方法要好。我认为看到this example 使用您的测试数据可以更好地解释我的意思。
  • @JamieBarker 我明白你的意思。我不确定 OP 是否需要维护对特定 div# 实例的引用。如果他不这样做,那么我建议使用您的示例代码。
【解决方案3】:

您需要使用 Each 并确保每个部分中的所有项目都具有相同的类。无需使用您当前使用的编号系统。

$('.MyDivs').each(function(index){ //Loop through all the elements with class of 'MyDivs'
   var thisElement = $(this); //get the current element we're on in the loop
   thisElement.find('.bokautbildning').slideToggle(); //Find the element with class 'bokautbildning' within the current element in the loop and slideToggle it
   thisElement.find('.boka_btn').click(function() { //Find the element with classs 'boka_btn' within the current element in the loop and add a click listener
      //Code for click event happens here
   });
}

这是一个例子:http://jsfiddle.net/o85tk0s3/

【讨论】:

  • 我在 PHP 代码中需要数字系统的另一个原因.. 但是感谢您的提示! :)
最近更新 更多