我目前正在试验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/)这样的问题,它专门检查代码并评论它。 –