【发布时间】:2012-05-08 22:08:32
【问题描述】:
我有两个具有相同类 .productList 的 div,并且想使用 jQuery 在两个 div 中操作第二个 <img>。
我有以下;
$('div.productList').each(function(){
$('img:last-child').css('margin-left','10px');
});
标记(部分)
<div class="productList">
<img src="images/parts/" width="120" height="120" alt="" title="" />
<img class="last" src="images/parts/" width="120" height="120" alt="" title="" />
我假设使用 .each() 会循环遍历每个 div 并找到最后一个 img,但这对我不起作用。
有人可以为我指出正确的方向吗?
谢谢。
【问题讨论】:
-
您通过 JS 而不是 CSS 这样做是有原因的吗?
div.productList img:last-child { margin-left: 10px } -
请展示一些示例标记
-
添加了标记。没有理由让 jQuery 超过 CSS。在这种情况下,我想使用 jQuery
-
我可以在那里看到一个
last类。那有什么问题呢?只需使用类选择器 (.last)。 -
我试图避免使用该类,而是使用 jQuery 来定位 img
标签: html css jquery-selectors jquery