我想在有人搜索特定类别的产品时显示一组特定的 Logo 。 Logo 图像元素最初是隐藏的(不显示),并且将具有类别 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);
此功能以模式显示 Logo ,除了在页面上显示 Logo 外,我希望能够执行相同的操作。
最佳答案
试试这个
function showPartnerLogos(element, cat_id) {
// then show the images with that class cat_id for that element
$(element + '.' + cat_id).show();
}
这将显示所有 img
元素以及在 cat_id
中发送的类。
理想情况下,您不需要参数 element
仅显示所有 img 标签,但是上面的代码可用于隐藏任何类型的元素,前提是您在参数元素中传递了标签名称
function showPartnerLogos(cat_id) {
// then show the images with that class cat_id for that element
$('img.' + cat_id).show();
}
如果这有帮助,请告诉我。
关于javascript - 使用JS显示依赖于元素类的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24321684/