我的問題是,這個jQuery的:動態XML流問題
<script type="text/javascript">
$(document).ready(function(){
$.get('CampusList.xml', function(xmltree){
$('#settings').before('<ul id="campuses">');
$(xmltree).find('campus').each(function(){
$('#settings').before('<li class="campus">'+$(this).attr('name')+'<ul id="buildings">');
$(this).find('building').each(function(){
$('#settings').before('<li class="building">'+$(this).attr('name')+'</li>');
});
$('#settings').before('</ul>');
$('#settings').before('</li>');
});
$('#sidebar').before('</ul>');
});
});
</script>
這是相關的XML:
<campuses name='Campuses'>
<campus name='Clearwater'>
<building name='Clearwater Building 1' img='someimg.png' />
<building name='Clearwater Building 2' img='someimg.png' />
<building name='Clearwater Building 3' />
</campus>
<campus name='Saint Petersburg'>
<building name='St. Pete Building 1' />
<building name='St. Pete Cafe Building' />
<building name='Other Building' />
<building name='Secret Chiller Plant Connection' />
</campus>
<campus name='Epicenter'>
<building name='District Offices' img='epi_do_floor1.png' />
<building name='Services' img='epi_services.png' />
<building name='Tech Building' img='epi_tech.png' />
</campus>
</campuses>
最後,這是錯誤的輸出:(我從chrome的檢查器截圖,因爲否則它將全部出現亂碼。)
請記住,您的解決方案將爲每個項目重新運行'.buildings:last'查詢,如果您運行一次並將其保存在變量中,則效率更高。很可能你不會注意到任何性能的提高,直到你得到幾百件左右的東西。 – DarthJDG
這很公平,我不確定變量聲明和實例化是否比選擇器查找效率低。 – Vap0r