Insert element between index x, y
I try to insert list item between list elem开发者_运维技巧ents:
$('#deals-providers dt').each(function(index) {
if (index == 10) {
$(this).insertBefore($('<dt/>').text('More item'));
}
});
but it doesn't work. How can I do this?
This does what you want:
$('#deals-providers dt:eq(9)').after('<dt>More item</dt>');
The :eq() selects the item at that index.
JSBin Example
try this:
edit:
$('dt:nth(10)').before('<dt>More item</dt>');
You could also use the :nth-child selector
See: http://jsfiddle.net/L2tk2/1/
$('#deals-providers dt').each(function(index) {
if (index == 10) {
$(this).insertBefore($('dt:contains("More item")'));
}
});
You can use the jQuery#eq
method to return an element at a designated index. This will return a jQuery object, unlike jQuery#get
, which returns an element node.
$('#deals-providers dt').eq(10).before(
$('<dt>').text('More text')
);
精彩评论