简体   繁体   English

获取同一类的多个元素-JavaScript(getElementsByClass)

[英]Get multiple elements same class - JavaScript (getElementsByClass)

I have an 'a href' that is a title. 我有一个标题为“ a href”。

<a href="#" class="title">Vendor1 product title</a>

I want to display an image based on the first word of the title. 我想显示基于标题的第一个单词的图像。

<a href="#" class="title">Vendor1 product title</a>
<div class="logo"></div>
<a href="#" class="title">Vendor2 product title</a>
<div class="logo"></div>
<a href="#" class="title">Vendor3 product title</a>
<div class="logo"></div>

These are item cells and they use the same template to be generated so the classes are always the same. 这些是项目单元格,它们使用相同的模板来生成,因此类别始终相同。 There are many of them. 有很多。 The script I have so far is working but only for the first product in the list (shows correct logo). 到目前为止,我拥有的脚本正在运行,但仅适用于列表中的第一个产品(显示正确的徽标)。

function getlogo() {

var string1 = document.getElementsByClassName('title')[0].innerHTML;
var vendor = string1.replace(/([a-z]+) .* ([a-z]+)/i, "$1").toLowerCase();

document.getElementsByClassName('logo')[0].innerHTML = '<img src="/myimages/' + vendor + '.jpg"  width="100px" height="50px" onerror="imgError(this);">';

function imgError(image) {
    image.onerror = "";
    image.src = "default.jpg";
    return true;
    }
}
getlogo();

I've looked around but sure how to loop this or even if that is the solution. 我环顾四周,但确定如何循环播放,即使这是解决方案。

http://jsfiddle.net/W7bm5/ http://jsfiddle.net/W7bm5/

It's easy if you use jQuery each function. 如果您使用jQuery每个函数,则很容易。

function imgError(image) {
     image.onerror = "";
     image.src = "default.jpg";
     return true;
}

$(document).ready(function() {
    $(".title").each(function() {
        var string1 = $(this).text();
        var vendor = string1.replace(/([a-z]+) .* ([a-z]+)/i, "$1").toLowerCase();
        $(this).html('<img src="/myimages/' + vendor + '.jpg"  width="100px" height="50px" onerror="imgError(this);">');
    });
});

or you can do it with the pure javascript, but put your logics in a loop, with [0] replaced to the loop index. 或者,您也可以使用纯JavaScript来做到这一点,但是将您的逻辑放入一个循环中,并用[0]替换为循环索引。

Update - here's how to keep the current text links: 更新-这是保持当前文本链接的方法:

function imgError(image) {
     image.onerror = "";
     image.src = "default.jpg";
     return true;
}

$(document).ready(function() {
    $(".title").each(function() {
        var string1 = $(this).text();
        var vendor = string1.replace(/([a-z]+) .* ([a-z]+)/i, "$1").toLowerCase();
        var html = $(this).parent().html();
        $(this).parent().html(html + '<br /><img src="/myimages/' + vendor + '.jpg"  width="100px" height="50px" onerror="imgError(this);">');
    });
});

You could use a for loop to run through the code you have for a different index of the getElementsByClassName results. 您可以使用for循环遍历具有getElementsByClassName结果的不同索引的代码。 See your jsFiddle 见你的jsFiddle

You could also ditch the getElementByClassName , which I think has spotty support in some browsers and isn't especially good performance, and navigate the DOM using JavaScript if your structure is always the same, or even jQuery if you'd like a library to make it easier. 您也可以放弃getElementByClassName ,我认为它在某些浏览器中支持不多,而且性能不是特别好,如果您的结构始终相同,则可以使用JavaScript来导航DOM;如果您想创建一个库,甚至可以使用jQuery来导航。更容易。

But by far the best is if you did it when it was generated in the first place. 但是到目前为止,最好的办法是一开始就生成它。 How are you generating the code and could you not use that to achieve what you are trying to achieve? 您如何生成代码,难道不能用它来实现您想要实现的目标吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM