JQuery .each只从自动生成元素数组中获取前两个元素

时间:2017-11-05 21:20:05

标签: jquery html css playframework

我有一个for循环,它使用类divs生成了一些panel

@for(comment <- event.getCommentsSorted()) {

现在我希望使用jQuery更改每个div,但.each只需使用类divs获取前两个panel

$(window).on('load', function() {
        $(".panel").each(function (index) {
            alert(index);
            $(this).height($(this)[index].scrollHeight - 12);
        });
    });

其他三个div不在那里。

enter image description here

我想也许是因为脚本是在生成所有divs之前执行的,但是load脚本应该在pageload之后执行。 我也尝试使用.ready或使用不同数量的生成divs但是我只获得前两个元素并不重要。

那么为什么我只得到第二个元素,有没有办法获得所有元素?

2 个答案:

答案 0 :(得分:0)

例如,您可以尝试将呼叫包裹在.each内的setTimeout

$(window).on('load', function() {
  setTimeout(function() {
    $(".panel").each(function (index) {
      alert(index);
      $(this).height($(this)[index].scrollHeight - 12);
    });
  }, 3000); // wait 3 seconds before executing
});

答案 1 :(得分:0)

我得到了答案,似乎

ServerElementComponent

只是不起作用。

我不知道为什么,但有了这一切都很好。

ServerElementComponent
相关问题