【问题标题】:For loop not working as expected with string replacement对于字符串替换,For 循环无法按预期工作
【发布时间】:2018-09-08 07:33:47
【问题描述】:

我正在尝试制作一个添加几个数字的 javascript webextension,例如。 “123”到购物网站上每个产品的超链接文本的内部文本的末尾,例如。 http://www.tomleemusic.ca

例如,如果我去这个链接,http://tomleemusic.ca/catalogsearch/result/?cat=0&q=piano

我想将商品的识别号添加到产品名称的末尾。

name of product and the href tag are in its item link, tomleemusic.ca/xxxxxx with the x's being the item number

但是,使用下面的代码,我只是将列表中第一个项目的项目编号附加到每个项目,而不是每个项目的不同项目编号。

var productsListLink = document.querySelectorAll(".products-grid .item .product-name a:not(.product-image)");
for (var i = 0; i < productsListLink.length; i++) {
    var a = productsListLink[i];
    var name = a.innerHTML || "";
    var addon = document.querySelector(".products-grid .item .product-name a:not(.product-image)").getAttribute('href');
    var newaddon = addon.replace("http://tomleemusic.ca/","");
    name += newaddon;
    a.innerHTML = name;
    a.setAttribute('title', name);
}

【问题讨论】:

    标签: javascript jquery for-loop google-chrome-extension


    【解决方案1】:

    在这一行中,您只抓取了 first 匹配元素:

    var addon = document.querySelector(".products-grid .item .product-name a:not(.product-image)").getAttribute('href')
    

    您已经在a 中获得了在每次循环迭代中实际使用的元素;只需使用它:

    var addon = a.getAttribute('href')
    

    例子:

    var productsListLink = document.querySelectorAll(".products-grid .item .product-name a:not(.product-image)");
    for (var i = 0; i < productsListLink.length; i++) {
        var a = productsListLink[i];
        var name = a.innerHTML || "";
        var addon = a.getAttribute('href');
        var newaddon = addon.replace("http://tomleemusic.ca/","");
        name += newaddon;
        a.innerHTML = name;
        a.setAttribute('title', name);
    }
    <div class="products-grid">
      <div class="item">
        <span class="product-name">
          <a href="http://tomleemusic.ca/1"></a>
        </span>
      </div>
      <div class="item">
        <span class="product-name">
          <a href="http://tomleemusic.ca/2"></a>
        </span>
      </div>
      <div class="item">
        <span class="product-name">
          <a href="http://tomleemusic.ca/3"></a>
        </span>
      </div>
    </div>

    【讨论】:

    • 谢谢!这很有帮助
    【解决方案2】:

    querySelector 将始终返回 first 匹配元素。因此,当你这样做时

    var addon = document.querySelector(".products-grid .item .product-name a:not(.product-image)").getAttribute('href');
    

    您正在选择第一个 a(您在第一次迭代中获得的那个)。

    但是,您可以通过使用数组方法和正则表达式来匹配id,从而使代码更加简洁:

    Array.prototype.forEach.call(
      document.querySelectorAll(".products-grid .item .product-name a:not(.product-image)"),
      (productNameElement) => {
        const idMatch = productNameElement.href.match(/\d+$/);
        if (idMatch) productNameElement.appendChild(document.createTextNode(idMatch[0]));
      });
    

    另请注意,只有 一些 元素有 ID 号。例如搜索结果之一:

    <a href="http://tomleemusic.ca/benchworld-sonata-1c-single-adjustable-artist-piano-bench-in-polished-walnut" title="BENCHWORLD SONATA 1c Single Adjustable Artist Piano Bench In Polished Walnut">BENCHWORLD SONATA 1c Single Adjustable Artist <span class="searchindex-highlight">Piano</span> Bench In Polished Walnut</a>
    

    没有,所以最好先检查是否有匹配项。

    【讨论】:

    • 请注意,NodeList#forEach 可能仍需要在使用中稍旧的浏览器上进行 polyfill; here's an answer of mine with a polyfill.
    • 我对@9​​87654329@ 部分没有按预期工作的问题没有印象......?
    • 如果 OP 正在寻找识别号,页面上的一些产品没有任何,所以当这些产品弹出时输出可能不符合预期,因此是正则表达式。不完全确定发生这种情况时需要什么
    • 是的,不清楚,但你的防御方法是有道理的。
    • 感谢您的详细解答!我几个月前才开始使用 java,所以我仍在努力清理我的代码 :) 这非常有用
    猜你喜欢
    • 1970-01-01
    • 2018-01-26
    • 1970-01-01
    • 1970-01-01
    • 2012-10-31
    • 1970-01-01
    • 1970-01-01
    • 2015-02-22
    • 2019-08-01
    相关资源
    最近更新 更多