2016-03-08 51 views
0

我试图抓住图像右侧的td =“description”标题/标题的内容 - 在表格中细胞。

然后将它添加到img下面并继续与之前相同的流程。

到目前为止,我有:

<script> 
$('tr').each(function() { 

var desc = $(this).$('td.description').html() 
    $(this).$('td.image').append(desc); 
    $('td.description').remove(); 


    }); 
$("table#shopping-cart-items tr td.image").after($('<td id="clearSpace"></td>')); 
}); 


    </script> 

如果我参加了“这个”了 - 它正确抓住它,并附加 - 但所有的名字都一样;所以我使用$(this)我假设是打破它,我还添加了$('tr')。每个函数?

谢谢您的时间!

+0

为什么不按照想要的方式构建HTML? – Mikey

回答

2

尝试使用$(this)得当,

$('tr').each(function() { 
    var desc = $('td.description', this).html() 
    $('td.image', this).append(desc); 
    $('td.description', this).remove(); 
}); 

你穿越的方式是错误的。默认情况下,在jquery包装器对象下没有名为$的函数。这就是为什么你的代码崩溃了。

+0

这正是我所需要的 - 道歉;我对这种变化以及如何正确使用这个问题感到困惑。 –

+0

@MikeVanStan,如果你阅读了API你会知道使用['find'](http://api.jquery.com/find),或者' $(selector,context)'(这是'$(this).find(selector)'的别名)。 – zzzzBov

+0

@MikeVanStan很高兴帮助! –

0

this里面的each功能是指当前正在迭代的项目匹配$('tr')。你的情况,你将有两种方式去:

  • $('td.description', this):匹配现有的this元素中的所有元素td.description

  • $(this).find('td.description'):同上。