-1

我希望当我们点击 Ajax 1 时,ajax1 的内容会发生变化,但 Ajax 2 将保持不变,反之亦然,请帮助我想用 jquery 做到这一点

<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#pas").click(function(){
  $.ajax({url:"001.php?q=1 & l=111", success:function(result){
    $("#pas").html(result);
  }});
});});
</script>
</head>
<body>
<div id="pas"><h2>AJAX 1</h2></div>
<div id="pas"><h2> AJAX2 </h2></div>
</body>
</html>
4

3 回答 3

2

两个元素不能有相同的id。您应该更改为使用类。

<div class="pas"><h2>AJAX 1</h2></div>
<div class="pas"><h2> AJAX2 </h2></div>

完成此操作后,您可以使用this来定位正确的项目。

$(document).ready(function(){
    $(".pas").click(function(){
        var $this = $(this);    // "this" is the clicked element

        $.ajax({url:"001.php?q=1 & l=111", success:function(result){
            $this.html(result);
        }});
    });
});
于 2012-04-25T21:25:34.503 回答
1

您必须区分 div 的 id:给它们两个不同的 id(即 pas1 和 pas2 )和相同的类“pas”,然后在 div.pas 上触发操作

于 2012-04-25T21:25:42.967 回答
0

不要使用重复的 id,而是使用类。使用对单击对象的引用来更新您的内容。这是您修改后的代码:

<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("div").click(function(){
  var _this = this;
  $.ajax({url:"001.php?q=1 & l=111", success:function(result){
    $(_this).html(result);
  }});
});});
</script>
</head>
<body>
<div><h2>AJAX 1</h2></div>
<div><h2> AJAX2 </h2></div>
</body>
</html>
于 2012-04-25T21:27:32.810 回答