代码之家  ›  专栏  ›  技术社区  ›  Deeksy

查找不存在的样式引用

css
  •  1
  • Deeksy  · 技术社区  · 16 年前

    有没有一个工具可以帮我找到我在HTML中引用的所有实际上不存在的CSS类?

    也就是说,如果我的HTML中有<ul class=“topnav”/>,并且任何引用的CSS文件中都不存在topnav类。

    这和 SO#33242 ,询问如何查找未使用的CSS样式。这不是一个重复的问题,因为那个问题问哪些CSS类不被使用。这是相反的问题。

    4 回复  |  直到 12 年前
        1
  •  4
  •   mislav Telemachus    16 年前

    function forItems(a, f) {
      for (var i = 0; i < a.length; i++) f(a.item(i))
    }
    
    function classExists(className) {
      var pattern = new RegExp('\\.' + className + '\\b'), found = false
    
      try {
        forItems(document.styleSheets, function(ss) {
          // decompose only screen stylesheets
          if (!ss.media.length || /\b(all|screen)\b/.test(ss.media.mediaText))
            forItems(ss.cssRules, function(r) {
              // ignore rules other than style rules
              if (r.type == CSSRule.STYLE_RULE && r.selectorText.match(pattern)) {
                found = true
                throw "found"
              }
            })
        })
      } catch(e) {}
    
    
      return found
    }
    
        2
  •  1
  •   Milan BabuÅ¡kov    16 年前

        3
  •  0
  •   Swati Markus    16 年前
        4
  •  0
  •   JSW189    12 年前