CSS último hijo (-1)
Estoy buscando un selector css que me permita seleccionar el pre-último hijo de list.
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li> <!-- select the pre last item dynamically no matter how long this list is -->
<li>6</li>
</ul>
Método Estático:
ul li:nth-child(5)
Método Dinámico:
ul li:last-child(-1)
Que por supuesto no valida, también nth-last-child no parece proporcionar una forma dinámica.. Puedo recurrir a javascript, pero me pregunto si hay una forma css que pasé por alto
104
Author: Hedde van der Heide, 2012-02-10
2 answers
Que puede uso :nth-last-child()
; de hecho, además de :nth-last-of-type()
no sé qué otra cosa se podría utilizar. No estoy seguro de lo que quiere decir con "dinámico", pero si se refiere a si el estilo se aplica al nuevo penúltimo hijo cuando se agregan más hijos a la lista, sí lo hará. Violín interactivo.
ul li:nth-last-child(2)
200
Author: BoltClock,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-02-12 16:13:58
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-02-12 16:13:58
A menos que pueda hacer que PHP etiquete ese elemento con una clase, es mejor usar jQuery.
jQuery(document).ready(function () {
$count = jQuery("ul li").size() - 1;
alert($count);
jQuery("ul li:nth-child("+$count+")").css("color","red");
});
1
Author: David Allen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-02-10 12:54:40
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-02-10 12:54:40