代码之家  ›  专栏  ›  技术社区  ›  George Johnston

嵌套每个条件

  •  1
  • George Johnston  · 技术社区  · 15 年前

    我有以下计时器定期更新我的页面:

    var refreshId = setInterval(function() {
        $(".content").each(function(i) {
            // Do stuff
            $(this).each(function(i) {
              // issue here
            });
        });
    }, 10000);
    

    在嵌套的foreach循环中,我只提取图像,因此本质上,我希望与此>.icons>.img匹配,因为我的图像位于“icons”类的一个分区中。

    该节的标记如下所示:

           <div class="content">
                <div></div>
                <div class="icons">
                    <img id="dynamicImage12345" src="#">
                </div>
            </div>
    

    我怎样才能做到这一点?

    2 回复  |  直到 15 年前
        1
  •  1
  •   Sarfraz    15 年前

    你需要这条线:

    $("div.icons > .img", $(this)).each(function() {
        // your code to for images
    });
    

    假设您正在使用 img 从代码中可以看到的图像的类。如果不使用类,可以尝试以下方法:

    $("div.icons > img", $(this)).each(function() {
        // your code to for images
    });
    

    于是它变成:

    var refreshId = setInterval(function() {
      $(".content").each(function(i) {
        // Do stuff
        $("div.icons > img", $(this)).each(function() {
          // your code to for images
        });
      });
    }, 10000);
    
        2
  •  0
  •   Powerlord    15 年前

    如果我读得对,你想要的是:

    var refreshId = setInterval(function() {
        $(".content").each(function(i) {
            // Do stuff
            $(".icons > .img", this).each(function(i) {
              // issue here
            });
        });
    }, 10000);