使用jQuery将非唯一HTML元素作为目标
Target non-unique HTML element with jQuery
我想将JS淡入/淡出应用于没有唯一ID或类的HTML元素。
<div class="view-content">
<table class="views-view-grid cols-3">
<tbody>
<tr class="row-1 row-first row-last">
<td class="col-1 col-first views-portals-row">
<div class="views-field views-field-title"> <h1 class="field-content view_portals_title"><a href="http://dev.test.eu/content/hydrography">Hydrography</a></h1> </div>
<div class="views-field views-field-field-attribution"> <div class="field-content view_portals_attr" style="display: none;"></div> </div>
<div class="views-field views-field-field-image-portal"> <div class="field-content view_portals_img"><a href="http://dev.test.eu/content/hydrography"><img width="261" height="200" alt="" src="http://dev.test.eu/sites/test.eu/files/public/hydrography.jpg" typeof="foaf:Image"></a></div> </div>
<div class="views-field views-field-body"> <div class="field-content view_portals_desc"><p>Data on bathymetry (water depth), coastlines, and geographical location of underwater features: wrecks.</p>
<p><a href="http://dev.test.eu/content/hydrography" class="button small">Read more</a></p>
</div> </div> </td>
<td class="col-2 views-portals-row">
<div class="views-field views-field-title"> <h1 class="field-content view_portals_title"><a href="http://dev.test.eu/content/geology">Geology</a></h1> </div>
<div class="views-field views-field-field-attribution"> <div class="field-content view_portals_attr" style="display: none;"></div> </div>
<div class="views-field views-field-field-image-portal"> <div class="field-content view_portals_img"><a href="http://dev.test.eu/content/geology"><img width="299" height="200" alt="" src="http://dev.test.eu/sites/test.eu/files/public/geology.png" typeof="foaf:Image"></a></div> </div>
<div class="views-field views-field-body"> <div class="field-content view_portals_desc"><p>Data on seabed substrate, sea-floor geology, coastal behaviour, geological events and probabilities, and minerals.</p>
<p><a href="http://dev.test.eu/content/geology" class="button small">Read more</a></p>
</div> </div> </td>
<td class="col-3 col-last views-portals-row">
<div class="views-field views-field-title"> <h1 class="field-content view_portals_title"><a href="http://dev.test.eu/content/seabed-habitats-0">Seabed Habitats</a></h1> </div>
<div class="views-field views-field-field-attribution"> <div class="field-content view_portals_attr" style="display: none;">Posidonia oceanica, © David Balata</div> </div>
<div class="views-field views-field-field-image-portal"> <div class="field-content view_portals_img"><a href="http://dev.test.eu/content/seabed-habitats-0"><img width="267" height="200" alt="" src="http://dev.test.eu/sites/test.eu/files/public/Posidonia%20oceanica_2.jpg" typeof="foaf:Image"></a></div> </div>
<div class="views-field views-field-body"> <div class="field-content view_portals_desc"><p>Data on modelled seabed habitats based on seadbed substrate, energy , biological zone, and salinity.</p>
<p><a href="http://dev.test.eu/content/seabed-habitats-0" class="button small">Read more</a></p>
</div> </div> </td>
</tr>
</tbody>
</table>
</div>
我使用以下脚本来隐藏和显示图片的属性:
(function ($) {
$(document).ready(function() {
$('.view_portals_attr').hide();
$('.views-portals-row').hover(function() {
$('.view_portals_attr').fadeIn();
}, function() {
$('.view_portals_attr').fadeOut();
});
});
})(jQuery);
问题是,当鼠标悬停在列表中的任何图片上时,都会显示所有属性。有人能告诉我如何一次瞄准一个吗?
您需要的是只选择子项,如下所示:
(function ($) {
$(document).ready(function() {
$('.view_portals_attr').hide();
$('.views-portals-row').hover(function() {
$(this).find('.view_portals_attr').fadeIn();
}, function() {
$(this).find('.view_portals_attr').fadeOut();
});
});
})(jQuery);
请参阅演示:http://jsfiddle.net/SVxKx/
相关文章:
- 如何覆盖锚点元素's href目标,并在我点击转到目标javascript时删除其他错误
- 任何一种简单的方法都可以将带有onload的元素作为目标
- AngularJs的ng-click$事件将子元素作为目标传递
- 从定位目标中的元素中清除类
- 查找元素的目标
- 指定数组中的元素对,其总和等于特定的目标数字
- 内容以其开头的目标元素
- 在 Jquery 中获取目标元素的属性
- 给定一个带有数字的数组,我如何编写一个递归函数,当 2 个元素加起来为一个目标时,它会在数组中查找索引
- 使用 jQuery 从目标页面而不是当前页面获取元素文本
- AngularJS:如何防止ng-click以子元素为目标
- 有人能向我解释一下:将元素附加到目标与将元素分配到目标
- window.onerror获取元素目标
- 使用next()的jQuery目标元素不起作用
- 以elementsByClassName数组的任何元素为目标
- ReactJS-目标随机元素
- 基于比较子元素内容和目标标题内容的Javascript排序元素
- Ember.js:在模板中呈现谷歌图表(仅当存在时才称为目标DOM元素)
- 在 HTA 中将元素目标发送到 iframe
- 事件委派 - 父元素目标而不是子元素