开发者

jQuery selector: which combination of jQuery selectors should I use to change the text between span tags

开发者 https://www.devze.com 2023-02-14 11:02 出处:网络
I have the following HTML: <tr> <td> <img id=\'1\' class=\'promote\' src=\'/images/plus.png\' />

I have the following HTML:

<tr>
  <td>
    <img id='1' class='promote' src='/images/plus.png' />
    <span>0</span>
    <img id='1' class='demote' src='/images/minus.png' />
  </td>
  <td>
    <img id='2' 开发者_运维技巧class='promote' src='/images/plus.png' />
    <span>0</span>
    <img id='2' class='demote' src='/images/minus.png' />
  </td>
...
</tr>

Next, i'm using jQuery ajax:

$('img.promote').click(function() {
  $.ajax({
    url: '/promote/' + this.id,
    success: function(data) {
      $(???).text(data.rating);
    },
    dataType: 'json'
  });
});
$('img.demote').click(function() {
  $.ajax({
  url: '/demote/' + this.id,
  success: function(data) {
    $(???).text(data.rating);
  },
  dataType: 'json'
  });
});

So, which combination of jQuery selectors should I use instead of "???" to change the text between span tags? or I'm doing it wrong at all?


You need to cache the span in the click handler

$('img.promote').click(function() {
  var $span = $(this).siblings('span');
  $.ajax({
    url: '/promote/' + this.id,
    success: function(data) {
      $span.text(data.rating);
    },
    dataType: 'json'
  });
});
0

精彩评论

暂无评论...
验证码 换一张
取 消