$('#button').click(function() {
alert($('.column').val());
});
我怎麼能得到.column
中的第一個,第二個或第三個元素?JQuery獲得陣列的第n個元素
$('#button').click(function() {
alert($('.column').val());
});
我怎麼能得到.column
中的第一個,第二個或第三個元素?JQuery獲得陣列的第n個元素
$('#button').click(function() {
alert($('.column').eq(0).val()); // first element
alert($('.column').eq(1).val()); // second
alert($('.column').eq(2).val()); // third
});
使用slice()函數: http://api.jquery.com/slice/
見問題: How to select a range of elements in jQuery
$('.column').slice(0, 2).each(function() {
$(this).val(); /* my value */
});
我喜歡選擇字符串,所以我通常這樣做:
$('.column:eq(3)') // Fourth .column element
我喜歡它們純粹是爲了風格,但應該指出的是,jQuery文檔推薦.eq()over:eq()以獲得現代瀏覽器的性能:http://api.jquery.com/eq-selector/ – Jeff
你可以使用:lt
(小於)選擇並告訴它你想0, 1, and 2
通過指示低於指數3
所有.column
元素:
$("#button").on("click", function(){
$(".column:lt(3)").each(function(){
alert(this.value);
});
});
你想第一,第二或第三層含義的任何三個?或者你想要所有的人?你是指在父類'.column'元素中的'.column'類或三個元素*的三個元素? – Sampson