我嘗試插入列表元素之間的列表項:插入元素,Y
$('#deals-providers dt').each(function(index) {
if (index == 10) {
$(this).insertBefore($('<dt/>').text('More item'));
}
});
,但它不工作。我怎樣才能做到這一點?
我嘗試插入列表元素之間的列表項:插入元素,Y
$('#deals-providers dt').each(function(index) {
if (index == 10) {
$(this).insertBefore($('<dt/>').text('More item'));
}
});
,但它不工作。我怎樣才能做到這一點?
試試這個:
編輯:
$('dt:nth(10)').before('<dt>More item</dt>');
我不認爲你想[10]那裏給你的dom元素,而不是集合中的第11個jQuery對象 - http://jsbin.com/ezopix/2/edit#preview –
@jadept不,這是因爲它試圖調用insertBefore在第11個dom元素,它沒有一個名爲insertBefore的方法。 –
$('#deals-providers dt').each(function(index) {
if (index == 10) {
$(this).insertBefore($('dt:contains("More item")'));
}
});
可以使用jQuery#eq
方法來指定索引處返回的元素。這將返回一個jQuery對象,不像jQuery#get
,它返回一個元素節點。
$('#deals-providers dt').eq(10).before(
$('<dt>').text('More text')
);
如何/在哪裏更新索引? – Nivas