首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用jquery每种方法为每个图像发布值?

如何使用jquery每种方法为每个图像发布值?
EN

Stack Overflow用户
提问于 2013-10-08 08:46:41
回答 1查看 139关注 0票数 1

我有html代码:

代码语言:javascript
复制
<div>
  <span class="image" id="img-1"></span>
  <span id="src-1">img-src-1</span>
  <span id="cmd-1">img-cmd-1</span>
  <span id="h1-1">img-h1-1</span>
  <span id="h2-1">img-h2-1>
</div>

<div>
  <span class="image" id="img-2"></span>
  <span id="src-2">img-src-2</span>
  <span id="cmd-2">img-cmd-2</span>
  <span id="h1-2">img-h1-2</span>
  <span id="h2-2">img-h2-2>
</div>

. others =‘others 5’>.有相关的价值

这是我的javascript:

代码语言:javascript
复制
$('.image').each(function() {
  $.post("../../includes/processes/show-images.php", {
    width : $(this).parent().width(),
    img_src : $(this).next().text(),
    cmd : $(this).next().next().text(),
    h1 : $(this).next().next().next().text(),
    h2 : $(this).next().next().next().next().text()
  },
  function(response){
    $(this).parent().html(response);
  });
});
return false;

我希望为每个".image“元素发布其值,并在".image".parent()中返回响应,但我的代码无法工作。:-(

有人能帮帮我吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-08 08:51:37

this关键字在$.post回调函数的上下文中不引用已单击的元素,缓存对象:

代码语言:javascript
复制
$('.image').each(function() {
   var $this = $(this);
   $.post("../../includes/processes/show-images.php", {
    // ...
   }, function(response){
       $this.parent().html(response);
   });
});

您还可以使用.siblings().eq()方法,而不是多次调用.next()方法:

代码语言:javascript
复制
$('.image').each(function() {
      var $this = $(this),
          $siblings = $this.siblings();
      $.post("../../includes/processes/show-images.php", {
          width   :  $this.parent().width(),
          img_src :  $siblings.eq(0).text(),
          cmd     :  $siblings.eq(1).text(),
          // ...
       }, function(response){
           $this.parent().html(response);
       });
});
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19243061

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档