简体   繁体   English

带有所有H标签的Javascript中的CSS选择器

[英]CSS Selectors in Javascript with all H tags

Using Prototype JS library, I want to select all child link elements ( A tags) regardless of whether their parent is: H1 , H2 , H3 , H4 , or H5 (etc) with a simple CSS Selector Rule (as opposed to further JS, like looping etc). 使用Prototype JS库,我想选择所有子链接元素( A标签),无论它们的父级是否为: H1H2H3H4H5 (等),带有简单的CSS选择规则 (与进一步的JS相反,像循环等)。

So the simple, but long way, to do this is: 所以这样做的简单但很长的方法是:

$('page').select('h1 > a, h2 > a, h3 > a, h4 > a, h5 > a')

I guess I'm looking for a wild-card property, like h*, that doesn't exist. 我想我正在寻找一个不存在的外卡属性,比如h *。

Maybe the above example is the way to go, but I'm hoping there is a simpler, more concise and efficient way to do this. 也许上面的例子是要走的路,但我希望有一个更简单,更简洁和有效的方法来做到这一点。

Tips appreciated. 提示赞赏。

As per Fabien Ménager comment to the original question, it looks like there isn't a simple CSS Selector I can use other than what I have already. 根据FabienMénager对原始问题的评论,看起来没有一个简单的CSS选择器我可以使用除了我已经使用的。

$('page').select('h1 > a, h2 > a, h3 > a, h4 > a, h5 > a')

While there are other options if I want to be programmatic about it, or empty JQuery (which isn't an option for me) I am specifically looking for a CSS rule. 虽然还有其他选择,如果我想对它进行编程,或者清空JQuery(这对我来说不是一个选项),我特意寻找CSS规则。

Thank you to everyone who tried to help. 感谢所有试图提供帮助的人。

使用jQuery:

$(":header a")

http://docs.jquery.com/Selectors/header says: http://docs.jquery.com/Selectors/header说:

:header Matches all elements that are headers, like h1, h2, h3 and so on. :header匹配所有标题元素,如h1,h2,h3等。

Adds a background and text color to all the headers on the page. 为页面上的所有标题添加背景和文本颜色。

$(":header").css({ background:'#CCC', color:'blue' });

got an answer to this from the prototype devs. 从原型开发者那里得到了答案。 these are best and only solutions, for now at least: 这些是最好的,也是唯一的解决方案,现在至少:

$$('h1 > a, h2 > a, h3 > a, h4 > a, h5 > a, h6 > a')
$('page').select('h1 > a, h2 > a, h3 > a, h4 > a, h5 > a')

However, the next release of Prototype will let you choose a selector engine and will default to Sizzle . 但是,Prototype的下一个版本将允许您选择一个选择器引擎,并默认为Sizzle The :header pseudo-selector is actually part of Sizzle so will be available in future versions of prototype (as long as you choose sizzle as your selector engine). :header伪选择器实际上是Sizzle的一部分,因此将在未来版本的原型中可用(只要你选择sizzle作为你的选择器引擎)。 I guess it was not previously included as it is not part of any specification. 我想之前没有包含它,因为它不是任何规范的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM