函数仅适用于使用.each函数的最后一个div

卡拉格莱克斯

我见过类似的案件,但我似乎无法弄清楚案件的情况。

因此,我得到了3个不同的div,它们都具有相同的“显示框”类。我想做的是将我的函数应用于所有3个div。目前,它仅将函数应用于最后一个div。

$(".display-boxes").each(function() {
  var boxList = $(this).context;

  function resizeTeasersTablet(teaserNumber, teaserCollection) {
    if (teaserNumber == 1) {
      teaserCollection[0].style.width = '73.834%';
      teaserCollection[0].style.margin = '0 auto 40px';
      for (let i = 1; i < teaserNumber; i++) {
        teaserCollection[i].style.width = '25%';
      }
    } else if (teaserNumber == 2) {
      for (let i = 0; i < teaserNumber; i++) {
        teaserCollection[i].style.width = '50%';
      }
    } else if (teaserNumber == 4) {
      for (let i = 0; i < teaserNumber; i++) {
        teaserCollection[i].style.width = '50%';
      }
    } else if (teaserNumber == 5) {
      for (let i = 0; i < teaserNumber; i++) {
        teaserCollection[i].style.width = '50%';
      }
    } else if (teaserNumber == 3) {
      for (let i = 0; i < teaserNumber; i++) {
        teaserCollection[i].style.width = '33.33%';
      }
    }
  }

  function resizeTeasersMobile(teaserNumber, teaserCollection) {
    for (let i = 0; i < teaserNumber; i++) {
      teaserCollection[i].style.width = '100%';
    }
  }

  document.addEventListener('DOMContentLoaded', function() {
    if (window.innerWidth > 2024) {
      displayTeasers(boxList.childElementCount, boxList.children);
    } else if (window.innerWidth > 641) {
      resizeTeasersTablet(boxList.childElementCount, boxList.children);
    } else {
      resizeTeasersMobile(boxList.childElementCount, boxList.children);
    }
  });

  window.onresize = function() {
    if (window.innerWidth > 2024) {
      displayTeasers(boxList.childElementCount, boxList.children);
    } else if (window.innerWidth > 641) {
      resizeTeasersTablet(boxList.childElementCount, boxList.children);
    } else {
      resizeTeasersMobile(boxList.childElementCount, boxList.children);
    }
  };
});

<div id="section2" class="padding-margin-border column">
    <div class="lobby-images-wrapper small-block-grid-1 display-boxes ongoingPromo">
        <div class="display-box">
            <div class="wrapper-lobby-image-item">
                <span class="image-wrapper">
                    <a href="@@Field.Teaser_Link@@">@@MarkComponentField(FieldPath+".Teaser_Image")@@
                        <img src="@@Field.Teaser_Image@@" data-original="@@Field.Teaser_Image@@"/>
                    </a>
                </span>
            </div>
        </div>
    </div>
</div>

这是我拥有的3个div之一,因为它不会让我发布更多代码。

穆罕默德·德汉(Mohammad Dehghan)

这里有两个问题:

  1. 您已经设置了window.onresize3次循环。只有最后一次分配才有效。您将覆盖前两个处理程序。这就是为什么您的更改仅应用于最后一个的原因div

  2. Document:DOMContentLoaded 完全加载和分析HTML文档时,将引发事件,这意味着:

    • 您的JavaScript代码在未加载文档时运行,因此jQuery选择器将找不到您的divs(似乎不是您的情况)。当您直接在文档正文开头的脚本中加载代码时,就会发生这种情况。
    • 您的JavaScript代码在文档加载后运行,因此您将找到所有元素,但是该事件已被触发,并且永远不会调用处理程序。当您将代码放在onload处理程序中时,会发生这种情况
    • 您的代码将在div创建s之后但未完全加载文档之前运行例如,如果您在end标记之前运行代码,则会发生这种情况</body>这是唯一可以正常工作的情况。您最好不要将代码置于如此危险之中!您的代码应健壮可靠。

修复

这是解决问题的方法(请密切注意我的评论):

// Define your utility functions at the root level

function resizeTeasersTablet(teaserCollection) {
    // No need to pass the collection size. It has a `length` property.
    let width = undefined;
    switch (teaserCollection.length) { // `swith`/`case` is simpler than those `if`s
        case 1:
            teaserCollection[0].style.width = '73.834%';
            teaserCollection[0].style.margin = '0 auto 40px';
            // The for loop is not needed. The length is 1!
            break;
        case 2:
        case 4:
        case 5:
            width = '50%';
            break;
        case 3:
            width = '33.33%';
            break;
    }
    if (width)
        for (let t of teaserCollection) // `for..of` is simpler
            t.style.width = width;
}

function resizeTeasersMobile(teaserCollection) {
    for (let t of teaserCollection)
        t.style.width = '100%';
}

// The function name is clear
function resizeBasedOnWindowWidth(boxList) {
    if (window.innerWidth > 2024)
        displayTeasers(boxList.children);
    else if (window.innerWidth > 641)
        resizeTeasersTablet(boxList.children);
    else
        resizeTeasersMobile(boxList.children);
}

// The function name is clear
function resizeAllBoxListsBasedOnWindowWidth() {
    $(".display-boxes").each(function () {
        resizeBasedOnWindowWidth(this);
    });
}

window.onresize = function () {
    this.resizeAllBoxListsBasedOnWindowWidth(); // Just call the defined function.
}

$(function () { // See here: https://api.jquery.com/ready/
    resizeAllBoxListsBasedOnWindowWidth(); // Do not repeat the code. Just call the function.
})

我在此代码中使用的原则非常重要。我建议您多次阅读代码:)

祝好运

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

我的函数仅适用于整数

Javascript:函数仅适用于getElementById

CopyFile函数仅适用于XP

Appendchild仅适用于最后一个元素

MutationObserver仅适用于最后一个突变

向量化R中的两个相似函数仅适用于一个

C ++构造函数仅适用于一审?

JQuery 函数仅适用于第一行

javascript函数仅适用于第一个元素

为什么JQuery函数仅适用于第一个文本框

jQuery函数仅适用于laravel中foreach的第一个元素

JS函数addEventListener仅适用于列表的第一个元素

Javascript函数仅适用于第一个元素

.each()中的setInterval仅适用于最后一个间隔?

jQuery HoverIntent仅适用于最后一个div

概念检查函数不适用于仅移动参数

仅适用于 alpha 和单个空格的函数

Lambdifying Sympy函数仅适用于实数

apply()函数仅适用于某些列

JavaScript 仅适用于一个 div

JavaScript 功能仅适用于一个 div,而不适用于整个文档

如何使用 Pandas 调用创建的函数适用于所有行(轴 = 1)但仅适用于数据帧的某些特定行?

最后一个孩子/最后一个类型不适用于 div

JavaScript函数仅适用于具有类名称的第一个元素,而不适用于具有该类的所有元素

.each函数不在最后一个元素上运行

onMouseover和onMouseout函数仅适用于我无序的项目列表中的一个列表项目

下面的 Excel VBA 函数仅适用于第一个 IF 条件,ELSEIF 不起作用

为什么我的 jQuery show() 函数在 php foreach 循环中仅适用于第一个元素

为什么最后一个函数也适用于第二次更改的其他元素