2011-12-28 53 views
2

我試圖加載的頁面是這樣的(雖然,用類的段落數「文本」每次都是不同的):jQuery的加載前十個元素與特定的類

<html> 
<body> 
<div id="container"> 
<p class="text">1</p> 
<p class="text">2</p> 
<p class="text">3</p> 
<p class="text">4</p> 
<p class="text">5</p> 
<p class="text">6</p> 
<p class="text">7</p> 
<p class="text">8</p> 
<p class="text">9</p> 
<p class="text">10</p> 
<p class="text">11</p> 
<p class="text">12</p> 
<p class="text">13</p> 
<p class="text">14</p> 
<p class="text">15</p> 
<p class="text">16</p> 
<p class="text">17</p> 
<p class="text">18</p> 
<p class="text">19</p> 
<p class="text">20</p> 
<p class="sometext">Some other text here</p> 
</div> 
</body> 
</html> 

是否有可能加載與jQuery像$('#text').load('other_file.html #container .p');之類的前10個段落?

回答

5

使用:lt()選擇器。它基於0,因此p:lt(10)應加載前10個元素。

$('#text').load('other_file.html #container p:lt(10)'); 

請記住整個響應仍在下載,只有一個小節通過選擇器顯示。

+0

如何我使用它來加載下一個10,所以從10-20? – 2017-02-04 12:29:46

2

你可能想使用:lt()選擇:

$('#text').load('other_file.html #container p:lt(10)'); 
0

可以使用CSS3,樣式選擇只加載第一10個元素:

$('#text').load('other_file.html #container .p:nth-child(-n+10)'); 

這將加載#container元素中的前10個元素。

文檔:http://api.jquery.com/nth-child-selector/(請注意,nth-child從1開始,而不是0)