在精簡程式碼與使用鏈式寫法的同時,可能會帶來程式碼難以閱讀的問題。適度加入縮排與換行,往往能起到很好的效果。
// 糟糕的寫法
$second.html(value);
$second.on('click',function(){
alert('hello everybody');
}).fadeIn('slow').animate({height:'120px'},500);
// 建議的寫法
$second.html(value);
$second
.on('click',function(){ alert('hello everybody');})
.fadeIn('slow')
.animate({height:'120px'},500);
更多高效 jQuery 實踐經驗。