简体   繁体   English

类型为nth的CSS选择器在<a>标签中</a>不起作用

[英]CSS selector with nth-of-type not working inside <a> tag

Before, I had this working: 以前,我有这个工作:

CSS 的CSS

.categorie-image:not(:first-of-type):not(:nth-of-type(2)):not(:nth-of-type(3)) {
    margin-top: 20px;
}

HTML 的HTML

<div class="categorie-image custom-border" style="background-image:url(succes.png);">
    <div class="categorie-title">Text</div>
</div>

But then I needed to make the divs clickable, so I wrapped them inside a <a href="#"> : 但是随后我需要使div可点击,因此我将它们包裹在<a href="#">

<a href="#">
    <div class="categorie-image custom-border" style="background-image:url(succes.png);">
        <div class="categorie-title">Text</div>
    </div>
</a>

But now my CSS is not working anymore. 但是现在我的CSS不再起作用了。 I know it has something to do with child elements, but I tried: 我知道这与子元素有关,但我尝试过:

  • a.categorie-image:not(:first-of-type)...
  • a > .categorie-image:not(:first-of-type)...
  • div.categorie-image:not(:first-of-type)...

But without luck. 但是没有运气。 How should I change my CSS? 我应该如何更改CSS?

The below selector doesn't work anymore because the div which has class='categorie-image' is the first of its type within this parent (which is the a ). 下面的选择器不再起作用,因为具有class='categorie-image'div是该父class='categorie-image'中的第一个类型(即a )。 So, the negation ( :not(:first-of-type) ) will prevent the element from being selected. 因此,否定( :not(:first-of-type) )将阻止选择元素。

.categorie-image:not(:first-of-type):not(:nth-of-type(2)):not(:nth-of-type(3)){
    margin-top: 20px;
}
<a href="#">
    <div class="categorie-image custom-border" style="background-image:url(succes.png);">
        <div class="categorie-title">Text</div>
    </div>
</a>

Note that even though you have attached the negations to a class selector, the nth-of-type selector actually works on the element type. 请注意,即使您将否定词附加到类选择器上,第nth-of-type选择器实际上也适用于元素类型。 So, your selector would select the first element of every type ( div , a , p etc) which are not the first or second or third of its type within each parent and also has the class .categorie-image . 因此,选择器将选择每种类型divap等)的第一个元素,该元素不是每个父对象中其类型的第一个,第二个或第三个元素,并且具有.categorie-image

Also, as Vitorino fernandes points out in his comment , it might be better to avoid chaining negation selectors and use nth-of-type(n+4) (or nth-child(n+4) whichever is appropriate for your case) to select all elements after the third. 此外,正如Vitorino fernandes 在其评论中指出的那样最好避免链接否定选择器,并使用nth-of-type(n+4) (或nth-child(n+4) (适合您的情况)选择第三个元素之后的所有元素。


While the above is the reason why the selector no longer works, I can only assume that prior to the a being added you had some other div element before the div with class='categorie-image' . 尽管上面是为什么选择不再工作,我只能假设之前的原因, a被添加你有一些其他div的前元素divclass='categorie-image'

It's not working because each <div class="categorie-image"> is now a child of the wrapping <a> . 它不起作用,因为每个<div class="categorie-image">现在都是包装<a>

You're best off using the <a> as the selector and using a child selector. 最好使用<a>作为选择器并使用子选择器。 Here's a fiddle: https://jsfiddle.net/zsbzkg0m/ 这是一个小提琴: https : //jsfiddle.net/zsbzkg0m/

a:not(:first-of-type):not(:nth-of-type(2)):not(:nth-of-type(3)) div.categorie-image{
    margin-top: 20px;
}

a tags are inline elements, div are block elements. 一个标签是内嵌元件,DIV是块元素。

It's not a good practice to put block elements inside your inline elements. 将块元素放入内联元素中不是一个好习惯。

But it's possible to just create a script to make the whole div clickable. 但是可以创建一个脚本来使整个div可点击。 Something like. 就像是。

$("div").click(function() {
    window.location = $(this).find("a").attr("href"); 
    return false;
});

HTML : HTML:

<div class="categorie-image custom-border" style="background-image:url(succes.png);">
    <div class="categorie-title">Text</div>
    <a href="#">Link</a>
</div>

This looks for a tags inside your div, and you can click anywhere on the div and it will redirect you to your link href value. 这看起来你的DIV 的标签,你可以在任何地方的div点击,它会重定向到您的链接href的值。

This case you won't have to change your CSS. 这种情况下,您不必更改CSS。 Just an option. 只是一个选择。

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

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