首页 > 解决方案 > 将 JavaScript 分配给某个 body 类

问题描述

我需要在特定页面上运行此脚本,但也只针对特定的正文类。示例:在此页面上有<body class="category-type-plp">

如何将我的脚本分配给“category-type-plp”?

plpSpaceRemove: function() {

    $(document).ready(function() {
          if ($('.bv-tile.bv-inline-rating-outer').length % 4 === 0) {
            for (let i = 0; i < ($('.bv-tile.bv-inline-rating-outer').length / 4); i++) {
              let remove = true;
              $('.bv-tile.bv-inline-rating-outer').slice(i * 4, 4 * (i + 1)).each(function() {
                  if ($(this).find(".bv-stars-wrap

                      ").length != 0) {
                      remove = false;
                    }
                  });
                if (remove) {
                  $('.bv-tile.bv-inline-rating-outer').slice(i * 4, 4 * (i + 1)).each(function() {
                      if ($(this).find(".bv-stars-wrap

                          ").length === 0) {
                          $(this).css("display", "none");
                        }
                      });
                  }
                }
              }
              else {
                for (let i = 0; i < ($('.bv-tile.bv-inline-rating-outer').length / 3); i++) {
                  let remove = true;
                  $('.bv-tile.bv-inline-rating-outer').slice(i * 3, 3 * (i + 1)).each(function() {
                        if ($(this).find('.bv-stars-wrap

                            ').length != 0) {
                            remove = false;
                          }
                        });

标签: javascripthtmlcss

解决方案


您可以检查您的身体是否有某个类别:

if (!$('body').hasClass('some-class')) return;

推荐阅读