【问题标题】:Use javascript to write CSS specificity function使用javascript编写CSS特异性函数
【发布时间】:2019-01-05 14:03:55
【问题描述】:

我需要编写一个 javascript 函数,它比较传入的两个参数。它们将是字符串。并表示 CSS 类。

该函数的目的是衡量 CSS 的特异性。因此,如果传入的参数是 CSS ID,则应该返回它而不是第二个参数。 (例如,如果 a = '#id',它将击败 b = '.red')。

有点迷失在我的 javascript 函数的最佳方法上。不确定是使用 if/else 语句还是 switch/case。无论哪种方式,它都会变得混乱,需要一些指示。

这是我想要做的:

  • 比较参数 a 和 b,两者都是字符串。
  • 如果输入的 css 类是一个 id,(例如,'#id'),这将胜过所有其他 css 类。所以返回这个。
  • 如果 css 类是标记名,ID 仍然胜过它。所以返回 ID。
  • 如果输入了“*”,则这没有特异性,因此任何其他类都优于它。
  • 类选择器胜过标记名。 (例如 div.red > p)。

    // '*' weakest - anything beats this
    // tagname beats '*'. EG 'div' beats '*'
    // 2 tagnames beats 1 tagname - EG 'div div' beats 'p'
    // class selector beats tagname - EG 'div.red' beats 'div'
    // id is strongest #id - '#ID' beats all the above
    
    
    function compare(a,b){
    
      const low = '*';
      const tagname = /^[a-zA-Z]+$/;
      const twoTagnames = /^[a-zA-Z\s]*$/;
    
    
      if (a === low) {
        a < b;
        return b;
      } else (b === low); {
        b < a;
        return a;
      }
    
      if (a.includes('#')) {
        a < b;
        return b;
     } else if (b.includes('#')) {
        b < a;
       return a;
     }
    
    }
    
    compare('*','#id');
    

【问题讨论】:

  • 我会使用以下文档作为参考:developer.mozilla.org/en-US/docs/Web/CSS/Specificity
  • @codeWonderland - 谢谢,我知道 css 的特殊性是什么。我需要帮助编写一个比较 css 类的 javascript 函数,并将其传递给一个 javascript 函数。返回权重较高的类。
  • @AuxTaco - 谢谢,但我没有使用 React。我需要在 Vanilla JS 中执行此操作!
  • @ReenaVerma 我认为过去发送的两个 cmets 并不是为了解决您的问题,它们只是关于如何开始自己构建它们的建议。如果它们是解决方案,它们会显示为答案,您可以标记为正确的解决方案

标签: javascript css-selectors string-comparison css-specificity


【解决方案1】:

我最近在一次求职面试中遇到了这个确切的问题,我能够这样想:

function compare(a, b) {
  let aItems = a.split([" "]);
  let aRating = [0, 0, 0];

  aItems.forEach((i) => {
    if (i.split("#").length > 1) {
      aRating[0] = aRating[0] + (i.split("#").length - 1);
    }

    if (i.split(".").length > 1) {
      aRating[1] = aRating[1] + (i.split(".").length - 1);
    }

    if (!i.startsWith("#") && !i.startsWith(".")) {
      aRating[2] = aRating[2] + 1;
    }
  });

  let bItems = b.split([" "]);
  let bRating = [0, 0, 0];

  bItems.forEach((i) => {
    if (i.split("#").length > 1) {
      bRating[0] = bRating[0] + (i.split("#").length - 1);
    }

    if (i.split(".").length > 1) {
      bRating[1] = bRating[1] + (i.split(".").length - 1);
    }

    if (!i.startsWith("#") && !i.startsWith(".")) {
      bRating[2] = bRating[2] + 1;
    }
  });

  if (aRating[0] > bRating[0]) {
    return a;
  } else if (bRating[0] > aRating[0]) {
    return b;
  }

  if (aRating[1] > bRating[1]) {
    return a;
  } else if (bRating[1] > aRating[1]) {
    return b;
  } else if (
    bRating[0] === aRating[0] &&
    bRating[2] === 0 &&
    aRating[2] === 0
  ) {
    return b;
  }

  if (aRating[2] > bRating[2]) {
    return a;
  } else if (bRating[2] > aRating[2]) {
    return b;
  }
}

但有一个问题,假设您正在比较tagnames(即htmlbody,其中越具体,特异性越高),那么您可能会遇到一些困难。我能够通过一系列 if else 测试破解我的方式。

【讨论】:

  • "html,正文越具体,特异性越高" 这不是真的。它们同样具体。
  • 根据测试,一个或另一个先例(我不记得是哪一个)。正如我所提到的,整个测试在 imo 中有点骇人听闻。但是,剩下的代码是合理的,你应该得到适当的特异性。
  • 啊,您指的是解决方案中的问题。我的错,我看错了。顺便说一句,我很羡慕你得到这样的面试问题!我会狼吞虎咽的。 (我是其他人在您的问题中链接到的答案的作者。)
猜你喜欢
  • 1970-01-01
  • 2021-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-11
  • 1970-01-01
  • 2012-01-10
  • 2019-10-09
相关资源
最近更新 更多