简体   繁体   English

在角度选择器上使用最后一个

[英]Use last-child on angular selector

How do I get the last-child CSS selector to work on an angular component selector? 如何让最后一个CSS选择器在角度组件选择器上工作? I want to add styles to the last component item with a particular class. 我想将样式添加到具有特定类的最后一个组件项中。

My HTML: 我的HTML:

<component-name class="abc"></component-name>
<component-name class="abc"></component-name>
<component-name class="abc"></component-name>
<component-name class="efg"></component-name>
<component-name class="efg"></component-name>

In the case above, I would like to style the last .abc item. 在上述情况下,我想为最后一个.abc项设置样式。 So my CSS: 所以我的CSS:

component-name.abc:last-child {
 // some styles
}

This doesn't work. 这行不通。 Neither does this: 这也没有:

component-name.abc:last-of-type {
    // some styles
}

How do I select the last-child in this case? 在这种情况下,如何选择最后一个孩子?

Is the css code you posted within the component-name.css -file? 您张贴在component-name.css -file中的CSS代码吗? If that's the case move it to the parent component style sheet or style.css . 如果是这种情况,请将其移至父组件样式表或style.css

You can use jQuery last() 您可以使用jQuery last()

Stack Snippet 堆栈片段

 $('component-name.abc').last().css( "background-color", "red" ) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <component-name class="abc">abc</component-name> <component-name class="abc">abc</component-name> <component-name class="abc">abc</component-name> <component-name class="efg">efg</component-name> <component-name class="efg">efg</component-name> 

Reference Link: 参考链接:

You could try the nth-of-type selector. 您可以尝试第n个类型选择器。

I made a fiddle: https://jsfiddle.net/34vwp9xd/ 我做了一个小提琴: https : //jsfiddle.net/34vwp9xd/

.abc:nth-of-type(3) {
  background:red;
}

But it's less than ideal because you have to know the index of the class you want to style, I think your going to need to use JS to solve this by adding a last class to the last element of class abc 但这并不理想,因为您必须知道要设置样式的类的索引,我认为您需要使用JS来解决此问题,方法是在abc类的最后一个元素中添加last

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

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