jQuery limit the number of elements shown to say l

2020-04-12 09:52发布

问题:

I got lets say this:

<ol>
<li>Content1</li>
    <li>Content2</li>
    <li>Content3</li>
    <li>Content4</li>
    <li>Content5</li>
    <li>Content6</li>
    <li>Content7</li>
    <li>Content8</li>
    <li>Content9</li>
    <li>Content10</li>
    <li>Content11</li>
    <li>Content12</li>
    <li>Content13</li>
</ol>

And the js something like this :

$("li").hide()
$("li:contains('Content')").show()

Qusetion :

Now How can i limit the number of shown elements to lets say around 5 ?

回答1:

$("ol li:contains('Content')").slice(0, 5).show();

or:

$("ol li:contains('Content'):lt(5)").show();


回答2:

try this-

$("ol li:contains('Content'):lt(5)").show();