我想在容器 div 中的现有元素之前添加一个元素。我尝试过三种不同的方法,但都失败了
方法一
$('.channel:first',$('#scroll_5')).before(print_nav);
方法2
$('.channel:eq(0)',$('#scroll_5')).before(print_nav);
方法3
$('.channel',$('#scroll_5')).each(function () {
$(this).before(print_nav);
return false;
});
容器 div 为 scroll_5
并具有类 channel
的元素。我想在顶部添加一个新 channel 。正确的做法是什么?
最佳答案
$('#scroll_5').prepend(print_nav);
参见http://api.jquery.com/prepend/
或者:
$(print_nav).prependTo('#scroll_5');
关于javascript - 在div顶部添加元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11142096/