我正在使用以下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();
});
});
我试图缩短脚本,但我无法理解。我觉得我在这里重复自己。
任何人都可以帮忙吗?
答案 0 :(得分: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();
})
})