You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求补全代码:分别统计各容器内.resp-container数量并分配对应类

Solution: Assign Classes to .resp-container Elements Based on Per-Container Count

It looks like your current code is calculating the total number of .resp-container elements across all containers, instead of counting them individually per parent container. That's why the class is being applied uniformly to all elements, not based on how many are in each container.

Here's the corrected approach that handles each container separately:

$(document).ready(function(){
  // Loop through each parent container (replace ".container" with your actual container selector)
  $(".container").each(function() {
    const $currentContainer = $(this);
    const $respElements = $currentContainer.find(".resp-container");
    const elementCount = $respElements.length;

    switch(elementCount) {
      case 1:
        $respElements.addClass("full-resp");
        break;
      case 2:
        $respElements.addClass("half-resp");
        break;
      case 3:
        $respElements.addClass("third-resp"); // Adjust class name as needed
        break;
      case 4:
        $respElements.addClass("quarter-resp"); // Add more cases for higher counts
        break;
      default:
        // Optional: Handle unexpected counts with a default class
        $respElements.addClass("default-resp");
        break;
    }
  });
});

Key Changes Explained:

  • .each() on containers: We iterate over each parent container instead of targeting all .resp-container elements at once. This lets us handle each group separately.
  • Per-container count: For each container, we find only its child .resp-container elements and count those, not the global total.
  • Targeted class addition: The class is added only to the elements inside the current container, based on that container's specific count.

Important Notes:

  • Replace .container with the actual CSS selector for your parent containers (e.g., .card, .section, or whatever wraps your .resp-container elements).
  • Add more case statements to the switch block if you need to handle counts greater than 4, using the appropriate class names for those cases.
  • The default case is optional but useful for handling unexpected counts (like 5+ elements) with a fallback class.

内容的提问来源于stack exchange,提问作者Aleks Pvn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:38:16