我目前正在試驗JQuery,並試圖讓自己的投資組合能夠改變點擊顯示的項目。我在那裏有很多重複的代碼,並且想要清理它,我知道我是如何在Java中使用它,但是我忘記了方法的名稱。如何清理我的Jquery代碼?
最後,我想有這樣的事情,但我忘了怎麼辦這正是:
$('.projects a[href^="#"]').on('click',function (e){
var href = $(this).attr('href');;
changePortfolio(String head, String text, String imgsource){
$(".description-head").html(head);
$(".description-text").html(text);
$('.preview').attr('src',imgsource);
}
if(href == "#project-portfolio"){
changePortfolio("Portfolio", "this is my portfolio", "bg.png");
}
我當前的代碼:
$('.projects a[href^="#"]').on('click',function (e){
var href = $(this).attr('href');;
if(href == "#project-portfolio"){
$(".description-head").html("PORTFOLIO WEBSITE");
$(".description-text").html("This is the portfolio website description");
$('.preview').attr('src','img/bg.jpg');
} else if(href == "#project-preview2"){
$(".description-head").html("PREVIEW 2");
$(".description-text").html("This is the preview 2 description");
$('.preview').attr('src','img/placeholder.jpg');
} else if(href == "#project-preview3"){
$(".description-head").html("PREVIEW 3");
$(".description-text").html("This is the preview 3 description");
$('.preview').attr('src','img/placeholder.jpg');
} else if(href == "#project-preview4"){
$(".description-head").html("PREVIEW 4");
$(".description-text").html("This is the preview 4 description");
$('.preview').attr('src','img/placeholder.jpg');
} else if(href == "#project-preview5"){
$(".description-head").html("PREVIEW 5");
$(".description-text").html("This is the preview 5 description");
$('.preview').attr('src','img/placeholder.jpg');
}
});
感謝您的閱讀:)
你可能想問[Codereview.SE](http://codereview.stackexchange.com/)這樣的問題,它專門檢查代碼並評論它。 –