javascript - 使用JS显示依赖于元素类的图像

标签 javascript jquery html css

我想在有人搜索特定类别的产品时显示一组特定的 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/

相关文章:

javascript - 如何在 PhantomJS 和 CasperJS 中设置 sessionStorage?

javascript - 显示一个值或另一个值,具体取决于它是否存在

php - 2 个表单调用相同的 Javascript 函数?

html - DIV 的 "position"属性的默认值是多少?

javascript - 为什么要在这个 var 声明中将这个变量赋值给它自己?

javascript - 每张图片都比前一张JS少

jquery - Chrome 中的 HTML5 音频控制问题

javascript - 将结果从 php if 语句传递到 javascript/html

javascript - Canvas 条形图动画

html - Bootstrap 中图标后内联显示的文本