Skip to content

Instantly share code, notes, and snippets.

@z2015
Created May 2, 2016 02:15
Show Gist options
  • Save z2015/e0eeaab33713609640bb886c54e69dae to your computer and use it in GitHub Desktop.
Save z2015/e0eeaab33713609640bb886c54e69dae to your computer and use it in GitHub Desktop.
jQuery获取ul元素内最后一行的所有元素
$('ul').each(function () {
var $lis = $('li', this);
var count = $lis.length;
if (count < 4) {
$lis.addClass('last-row');
} else {
var numberInLastRow = count % 3 || 3;
$lis.eq(-1 * numberInLastRow - 1).nextAll().addClass('last-row');
}
});
<ul>
<li>1</li>
<li>2</li>
</ul>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
</ul>
ul {
width:300px;
}
li {
display:inline-block;
width:90px;
border:1px solid;
margin:1px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment