使用JS显示依赖于元素类的图像
Use JS to show images dependant on element class
我想在某人搜索特定类别中的产品时显示一组特定的徽标。logo图像元素最初是隐藏的(显示为none),并将有一个类来表示类别id。类别决定了显示什么标志。我的一个标志的html是:
<li id="show-logo" style="display: none;">
<!-- PLaytex Logo -->
<img src="/images/logos/playtex.jpg" class="cat_1">
</li>
所以我的函数应该如下:
showPartnerLogos(element, cat_id);
function showPartnerLogos(element, cat_id) {
// then show the images with that class cat_id for that element
}
我没有做任何代码,因为我的JS是惨淡的,但这个函数是目前被用来显示一个模态时,有人搜索产品:
$('#modal .spinner').spin('flower', 'black');
// Loop through all selected categories
$("#form_buy input[name='buy_product']:checked").each(function() {
$('#modal').find('.cat_' + $(this).val() ).show();
});
setTimeout(function() {
document.location.href = $(form).attr('action');
},0);
这个函数显示在一个模态的标志,我希望能够做同样的除了显示在页面上的标志。
试试这个
function showPartnerLogos(element, cat_id) {
// then show the images with that class cat_id for that element
$(element + '.' + cat_id).show();
}
这将显示cat_id
中发送的类的所有img
元素。
理想情况下,你不需要参数element
只显示所有的img标签,但上面的代码可以用来隐藏任何类型的元素,前提是你在参数element
function showPartnerLogos(cat_id) {
// then show the images with that class cat_id for that element
$('img.' + cat_id).show();
}
如果有帮助,请告诉我。
它会像这样:
function showPartnerLogos(element, cat_id) {
$(element+"."+cat_id).show();
}
应该这样调用:
showPartnerLogos("img", "cat_1");
显示与cat_1类匹配的所有图像元素。诀窍是使用最终匹配器:"img。
相关文章:
- 使用 jQuery 从 HTML 中的多个图像元素中删除文件路径
- 将图像元素添加到<a>标签
- 我在jQuery事件处理和隐藏/显示图像元素方面做错了什么
- 将带有图像元素的svg转换为html画布
- 如何获取画布中图像元素的坐标
- jQuery将“x”号图像元素应用于SRC和类
- 使用 DOM 方法删除图像元素
- PHP 中图像元素上的 OnClick 事件不起作用
- 通过 html 图像元素对象显示图像
- 如何将<图像>元素添加到 SVG DOM
- 选择具有特定 src 且没有 alt 标记的所有图像元素
- JavaScript:在键盘插入符号处插入现有图像元素旁边的图像元素
- 如何一次性创建图像元素及其属性
- 在浏览器中移动图像元素
- 将上传的文件添加为 SVG 中的<图像>元素
- 图像元素“清除”背景颜色不起作用
- 为什么画布绘制图像在使用 dom 图像元素时不起作用
- 如何给图像元素一个“;选择“;使用Raphael.js查看
- 关于为Raphaë添加淡入淡出效果的问题;l.js图像元素
- 如何重置图像元素's宽度