即使在其中1个附加了1个元素之后,如何总是出现3个元素?

发布于 2025-01-27 17:59:08 字数 1221 浏览 2 评论 0原文

我只需要最大3个元素即可始终出现在屏幕上,如果其中一个元素被删除,那么下的下一个元素将其位置,依此类推,直到没有元素为止。

这是代码段:

<div class="mob-keys-container" id="mob-keys-container">
        <img class="answerIcon mobIcon active" draggable="true" id="answer_1_mob" src="./images/5.1.png" alt="key">
        <img class="answerIcon mobIcon active" draggable="true" id="answer_2_mob" src="./images/5.2.png" alt="key">
        <img class="answerIcon mobIcon active" draggable="true" id="answer_3_mob" src="./images/5.3.png" alt="key">
        <img class="answerIcon mobIcon active" draggable="true" id="answer_4_mob" src="./images/5.4.png" alt="key">
        <img class="answerIcon mobIcon active" draggable="true" id="answer_5_mob" src="./images/5.5.png" alt="key">
        <img class="answerIcon mobIcon active" draggable="true" id="answer_6_mob" src="./images/5.6.png" alt="key">

 </div>

这是有效的最佳代码:

$('.info-mob').on('click', () => {
        $('#mob-keys-container').toggle();
        $('#mob-keys-container').children().slice(0, 3).show();
    });

.info-mob打开一个包含所有元素的选项卡,虽然打开,但应有最大3个元素。如果我再次关闭并打开该选项卡,一切正常,但是即使我附加具有不同函数的元素,我也需要它更具动态性,并且始终是最大3个元素。

I need only max 3 elements to appear on the screen at all times and if one of them is removed, then the next in line takes its place and so on till the are no elements.

Here is the code snippet:

<div class="mob-keys-container" id="mob-keys-container">
        <img class="answerIcon mobIcon active" draggable="true" id="answer_1_mob" src="./images/5.1.png" alt="key">
        <img class="answerIcon mobIcon active" draggable="true" id="answer_2_mob" src="./images/5.2.png" alt="key">
        <img class="answerIcon mobIcon active" draggable="true" id="answer_3_mob" src="./images/5.3.png" alt="key">
        <img class="answerIcon mobIcon active" draggable="true" id="answer_4_mob" src="./images/5.4.png" alt="key">
        <img class="answerIcon mobIcon active" draggable="true" id="answer_5_mob" src="./images/5.5.png" alt="key">
        <img class="answerIcon mobIcon active" draggable="true" id="answer_6_mob" src="./images/5.6.png" alt="key">

 </div>

And here is the best code that kinda works:

$('.info-mob').on('click', () => {
        $('#mob-keys-container').toggle();
        $('#mob-keys-container').children().slice(0, 3).show();
    });

.info-mob opens a tab that contains all the elements and while it's open there should be max 3 elements. Everything works fine if I close and open up again that tab but I need it to be more dynamic and always be max 3 elements even if I append one with different function.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月寒剑心 2025-02-03 17:59:08

我认为这将在CSS的帮助下解决您的问题

#mob-keys-container *{
  display:none;
}

#mob-keys-container *:nth-child(-n+3){
  display:block;
}

I think this will solve your issue with help of CSS

#mob-keys-container *{
  display:none;
}

#mob-keys-container *:nth-child(-n+3){
  display:block;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文