【问题标题】:Adding content by searching elements href using Javascript通过使用 Javascript 搜索元素 href 添加内容
【发布时间】:2021-10-10 07:25:26
【问题描述】:

我有一个男士和女士产品列表,在每个产品下方使用 Javascript,我想打印一条消息,告诉用户这些产品有任何尺寸的库存。为了确定哪些产品是男性和女性,我正在查看 href 并定位“womensclothing”和“mensclothing”,它们有效,但是当我去打印消息时,它们并没有正确地位于产品下方,正如你看到的那样运行代码。对此的任何帮助将不胜感激。

请注意,我无权访问 html 进行更改。

var womensClothing = document.querySelectorAll('.productTitle[href*="womensclothing"]');
var womenSizeLocation = document.querySelectorAll('.productPrice');
womensClothing.forEach(function(link, i) {
  womenSizeLocation[i].insertAdjacentHTML("afterend", "<a class='sizeAvailability' href=" + link.getAttribute('href') + "><p>Women's sizes:xs, s, m, l</p></a>");
});


var mensClothing = document.querySelectorAll('.productTitle[href*="mensclothing"]');
var menSizeLocation = document.querySelectorAll('.productPrice');
mensClothing.forEach(function(links, el) {
  menSizeLocation[el].insertAdjacentHTML("afterend", "<a class='sizeAvailability' href=" + links.getAttribute('href') + "><p>Men's Sizes: s, m, l, xl </p></a>");
});
.productWrapper {
  margin: 0 0 10px;
  border: 1px solid black;
}
<div class="productWrapper">
  <a class="productTitle" href="/mensclothing/">Mens Product</a>
  <div class="productPrice">€35,00</div>
</div>

<div class="productWrapper">
  <a class="productTitle" href="/mensclothing/">Mens Product</a>
  <div class="productPrice">€35,00</div>
</div>

<div class="productWrapper">
  <a class="productTitle" href="/womensclothing/">Womens Product</a>
  <div class="productPrice">€35,00</div>
</div>

<div class="productWrapper">
  <a class="productTitle" href="/womensclothing/">Womens Product</a>
  <div class="productPrice">€35,00</div>
</div>

【问题讨论】:

    标签: javascript dynamic href


    【解决方案1】:

    由于所有产品的价格类别都相同:productPrice,因此您需要使用 CSS Sibling 运算符 ~ 来选择与特定 &lt;a&gt; 元素同级的元素。

    var womensClothing = document.querySelectorAll('.productTitle[href*="/womensclothing"]');
    var womenSizeLocation = document.querySelectorAll('.productTitle[href*="/womensclothing"] ~ .productPrice');
    womensClothing.forEach(function(link, i) {
      womenSizeLocation[i].insertAdjacentHTML("afterend", "<a class='sizeAvailability' href=" + link.getAttribute('href') + "><p>Women's sizes:xs, s, m, l</p></a>");
    });
    
    
    var mensClothing = document.querySelectorAll('.productTitle[href*="/mensclothing"]');
    var menSizeLocation = document.querySelectorAll('.productTitle[href*="/mensclothing"] ~ .productPrice');
    mensClothing.forEach(function(links, el) {
      menSizeLocation[el].insertAdjacentHTML("afterend", "<a class='sizeAvailability' href=" + links.getAttribute('href') + "><p>Men's Sizes: s, m, l, xl </p></a>");
    });
    .productWrapper {
      margin: 0 0 10px;
      border: 1px solid black;
    }
    <div class="productWrapper">
      <a class="productTitle" href="/mensclothing/">Mens Product</a>
      <div class="productPrice">€35,00</div>
    </div>
    
    <div class="productWrapper">
      <a class="productTitle" href="/mensclothing/">Mens Product</a>
      <div class="productPrice">€35,00</div>
    </div>
    
    <div class="productWrapper">
      <a class="productTitle" href="/womensclothing/">Womens Product</a>
      <div class="productPrice">€35,00</div>
    </div>
    
    <div class="productWrapper">
      <a class="productTitle" href="/womensclothing/">Womens Product</a>
      <div class="productPrice">€35,00</div>
    </div>

    【讨论】:

    • 你刚刚教了我关于 CSS 兄弟运算符的知识,很好的答案,点个赞!
    • 啊是的,这很完美,从来不知道你能做到这一点!感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-10
    • 2015-10-17
    • 1970-01-01
    • 1970-01-01
    • 2015-10-22
    • 2019-09-18
    相关资源
    最近更新 更多