-2

我正在使用以下 jQuery 代码

$(document).ready(function() {
    $("#a").click(function(evt) {
         $("#bodyR").load("./inc/backa.php")
         evt.preventDefault();
    });
    $("#b").click(function(evt) {
       $("#bodyR").load("./inc/backb.php")
       evt.preventDefault();
    });
    $("#c").click(function(evt) {
       $("#bodyR").load("./inc/backc.php")
       evt.preventDefault();
    });
});

我试图缩短脚本,但我无法弄清楚。我觉得我在这里重复自己。

有人能帮忙吗?

4

1 回答 1

7

你可以这样做 :

$(document).ready(function() {
  $("#a,#b,#c").click(function(evt) { // comma selector to target more than one element
     $("#bodyR").load("./inc/back"+this.id+".php") // path built using the element's id
     evt.preventDefault();
  })
})
于 2013-04-19T15:33:13.063 回答