Jak wybrać ostatnie 2 pozycje na liście za pomocą css nth-child?
Czy to możliwe? Jeśli nie, czy istnieje sposób, aby to zrobić z jQuery?
22
3 answers
niestety to niemożliwe.. Zignoruj tę odpowiedź i spójrz na odpowiedź spliterów poniżej.
ul li:last-child+li {...}
Ale to nie działa, ponieważ + wybierze najbliższe rodzeństwo po last-child (co oczywiście nic nie znaczy). Nie ma bezpośredniego poprzedniego selektora.
Istnieją różne sposoby osiągnięcia tego z jQuery, najbardziej wydajny byłby be...
var lastItems = $("#list li");
lastItems.slice(lastItems.length - 2).addClass("whatever");
2
Author: Duopixel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-02-20 09:38:43
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-02-20 09:38:43
Jest to możliwe z CSS3. Uwaga na znaczniki:
<ul>
<li><a href="">First item</a></li>
<li>Second item</li>
<li><a href="">Test</a></li>
<li><a href="">Test</a></li>
<li><a href="">Pre-last item</a></li>
<li><a href="">Last item</a></li>
</ul>
Aby wybrać dwa ostatnie elementy LI należy:
ul > li:nth-last-of-type(-n+2) {
background: green;
}
Działa we wszystkich współczesnych przeglądarkach, w tym IE9, ale z wyłączeniem IE8 i poniżej. Aby dodać obsługę tych przeglądarek, można rozważyć użycie Selectivizr .js
125
Author: spliter,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-05-16 13:02:49
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-05-16 13:02:49
:last-child,
:nth-last-child(2) {
ruleset
}
Zagra również.
14
Author: katsampu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-04-23 08:46:32
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-04-23 08:46:32