最後一個項目我做了以下內容:空()追加()附加在循環
$('.document-content').on('input', function() {
var headers;
headers = $('.document-content > h2').each(function() {
var headerId, headerText;
headerId = $(this).attr('id');
headerText = $(this).text();
$('.document-outline').empty().append("<h2><a href='#" + headerId + "'>" + headerText + "</h2>");
});
});
爲了避免重複,我加了empty()
但在每個loop
現在只有最後一個項目被附於.document-outline
。
我該如何解決這個問題?