【问题标题】:jQuery selector: which combination of jQuery selectors should I use to change the text between span tagsjQuery 选择器:我应该使用哪种 jQuery 选择器组合来更改跨度标签之间的文本
【发布时间】:2011-07-08 06:05:54
【问题描述】:

我有以下 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>

接下来,我正在使用 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'
  });
});

那么,我应该使用哪种 jQuery 选择器组合来代替“???”更改跨度标签之间的文本?还是我做错了?

【问题讨论】:

  • 您是否尝试一次更改所有个跨度?还是只是一个特定的?
  • 只有一个,它是评分系统的一部分。

标签: jquery ajax css-selectors


【解决方案1】:

您需要在点击处理程序中缓存跨度

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多