【问题标题】:Custom Element Selector自定义元素选择器
【发布时间】:2015-10-07 11:27:33
【问题描述】:

有没有办法用 CSS 选择所有自定义元素?我想让所有自定义元素默认成为块元素(大多数浏览器默认将它们内联),然后根据需要覆盖它。

我的规则可能如下所示:

*::custom {
    display: block;
}

所有自定义元素在标准中都有破折号,所以我可以利用它创建一个规则,但在许多/大多数当前浏览器上会比较慢,因为它需要使用正则表达式。如果有内置的选择器,这可能会更快。

【问题讨论】:

  • 那么您的项目中有多少自定义元素? 5、10、20?手动编写规则就更简单了:x-one, x-two, ..., x-xxx {display: block;}.
  • AFAIK,没有这样的伪选择器,CSS 也不支持选择器中的部分通配符名称(尽管这是一个明显的增强)。
  • “大多数浏览器默认内联”:这是因为display的初始值是inline
  • 不能加classdata-属性吗?
  • @divinecomedian,正如 Angular 文档所说(这与指令有关,但也可以应用于元素/属性,因为指令就是这样):“在创建组件时使用元素控制模板。这种情况的常见情况是您为模板的某些部分创建域特定语言。当您用新功能装饰现有元素时使用属性。"

标签: html css css-selectors custom-element


【解决方案1】:

不,没有伪选择器可以做到这一点。

然而,一个肯定不是最佳解决方案的方法是使用这种类型的 CSS:

:not(html, head, body, h1, h2, h3, h4, h5, h6, div, ...) {
  /* Code here */
}

它会工作的!不利的一面是,如果添加了新元素,您需要将该元素添加到您的非选择器中。耶。

^.^

【讨论】:

    【解决方案2】:

    为您的自定义元素添加惰性自定义属性:

    <some-element cutom-elem /> <other-element custom-elem />
    <script> 
      var customs = document.querySelectorAll( "*[custom-elem]" )
    </script>
    <style>
        *[custom-elem] { display: block ; }
    </style>
    

    【讨论】:

    • 这可以工作(尽管我会将data- 放在自定义属性的开头),并且与我所做的类似。现在我删除了许多自定义元素,因为我意识到一些内置元素是可以的。我宁愿不要添加更多的属性。
    【解决方案3】:

    这是基于 Florrie 回答的解决方法: :not(html):not(head):not(title):not(base):not(link):not(meta):not(style):not(body):not(article):not(section):not(nav):not(aside):not(h1):not(h2):not(h3):not(h4):not(h5):not(h6):not(hgroup):not(header):not(footer):not(address):not(p):not(hr):not(pre):not(blockquote):not(ol):not(ul):not(li):not(dl):not(dt):not(dd):not(figure):not(figcaption):not(div):not(main):not(a):not(em):not(strong):not(small):not(s):not(cite):not(q):not(dfn):not(abbr):not(data):not(time):not(code):not(var):not(samp):not(kbd):not(sub):not(sup):not(i):not(b):not(u):not(mark):not(ruby):not(rb):not(rt):not(rtc):not(rp):not(bdi):not(bdo):not(span):not(br):not(wbr):not(ins):not(del):not(picture):not(img):not(iframe):not(embed):not(object):not(param):not(video):not(audio):not(source):not(track):not(map):not(area):not(math):not(svg):not(table):not(caption):not(colgroup):not(col):not(tbody):not(thead):not(tfoot):not(tr):not(td):not(th):not(form):not(label):not(input):not(button):not(select):not(datalist):not(optgroup):not(option):not(textarea):not(keygen):not(output):not(progress):not(meter):not(fieldset):not(legend):not(script):not(noscript):not(template):not(canvas)

    此外,您还必须考虑 SVG 和 MathML 命名空间。

    • 一种方法是以类似的方式添加它们的标签。
    • 在某些情况下(广告拦截),在该选择器前面加上几个可能的父级可能就足以避免 子级。像:-webkit-any(body, div) &gt; :not(... 这样的东西。见:is(), :matches(), :any()
    • 一旦实施,4 级选择器应该允许:not(math *, svg *) 之类的东西。
    • 可以使用@namespace,例如@namespace xhtml "http://www.w3.org/1999/xhtml"; xhtml|*:not(...

    【讨论】:

    • 这个选择器的特异性.....每个 :not() 是另外 10 个特异性点。击败该选择器的唯一方法是使用 ID。
    【解决方案4】:

    您可以使用来自this answer 的稍微修改的代码来获取所有已注册的自定义标签名称:

    function getCustomElements() {
        const allElems = document.getElementsByTagName("*");
        let elementNames = [].map.call(allElems, el => el.nodeName.toLowerCase())
        elementNames = [...new Set(elementNames)];
        return elementNames.filter(name => customElements.get(name));
    }
    

    然后您可以使用它来设置所有自定义元素的样式:

    const customElementSelector = getCustomElements().join();
    document.querySelectorAll(customElementSelector).forEach(el => {
        el.style.border = "solid";
    });
    

    【讨论】:

    【解决方案5】:

    您可以像下面这样简单地使用 css:

    custom-element{
        color: white;
        min-height: 20px;
    }
    

    我已经在 Firefox 和 Chrome 中对此进行了测试。虽然不确定实际的兼容性。请在您的环境中进行测试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-02
      • 2016-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-16
      相关资源
      最近更新 更多