我想将从此AJAX调用返回的结果数量限制为5.我想我可以使用.slice()来执行此操作,但我不确定该采用何种方式。
$(document).ready(function() {
$.ajax({
type: "GET",
url: "myurl",
dataType: "xml",
cache: false,
success: parsePopular
});
});
function parsePopular(popular) {
$(popular).find("item").each(function() {
$("#popularArticles").append("<h1><p class='Bold'><a href='" + $(this).find("link").text() + "'>" + $(this).find("title").text() + "</a></p></h1>");
});
}
任何想法都会很棒。
感谢。
答案 0 :(得分:2)
$("div").slice(0, 5).remove()
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<div>0</div>
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
&#13;
<div ng-app="myApp" ng-controller="myController">
<select name="vchOfficeStateVchID" ng-model="form.vchOfficeStateVchID" ng-options="s.StateVchID as s.Name for s in states">
<option value="">--Select State--</option>
</select>
<br/>
<div>
<h3>
Selected ID:
</h3>
{{form.vchOfficeStateVchID}}
</div>
</div>
&#13;
答案 1 :(得分:1)
切片并删除
$(popular).find('item').slice(5).remove();