私は現在、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/)でこのような質問をしたい場合があります。 –