使用javascript顯示和隱藏同一類的顯示

我正在嘗試使用javascript選擇要顯示和隱藏的元素,我無法對html進行更改,下面的javascript show error on“Cannot set property'display'of undefined”。如何顯示和隱藏共享同一類的元素?

var showHideBrandPic = document.querySelectorAll("h1.plp_brand_title");
var pic = document.querySelectorAll('.hero_discription_container_left');


function showHidePic (){
    showHideBrandPic.forEach(showHideBrandPic => {
        if(showHideBrandPic.textContent === "ADIDAS BY PHARRELL WILLIAMS"){
            pic.style.display = "none";
        }else {
            pic.style.display = "block";
        }
    })
   
};

showHidePic();
<h1 class="plp_brand_title">abc</h1>
    <img class="hero_discription_container_left" src="/qw.png" alt="123" width="500" height="600">
<h1 class="plp_brand_title">ADIDAS BY PHARRELL WILLIAMS</h1>
    <img class="hero_discription_container_left" src="/qw.png" alt="123" width="500" height="600">
? 最佳回答:

這增加了一層功能,允許您設置一個文本數組來查找和隱藏圖像。

這個方法依賴于在classnames數組中的h1-with類之后總是有一個類為.hero_discription_container_left的映像。你把它們放在一個數組里,這樣就很容易把它們聯系起來。

window.addEventListener('load', () => {
  let removeImagesAfter = ["remove my image too", "remove ME", "ADIDAS BY PHARRELL WILLIAMS"].map(e => e.toLowerCase()); // for sake of comparisons, we convert these to lower case 
  const classNames = ["plp_brand_title", "otherclass", "otherclass2"]
  let h1s = document.querySelectorAll("h1")
  let imgs = document.querySelectorAll("img.hero_discription_container_left")
  h1s.forEach((h1, i) => {
    if (classNames.filter(c => h1.classList.contains(c)).length === 0) return;
    imgs[i].style.display = removeImagesAfter.includes(h1.innerText.trim().toLowerCase()) ? 'none' : 'block';
  })
})
<h1 class="otherclass2">remove my image too</h1>
<img class="hero_discription_container_left" src="/qw.png" alt="123" width="500" height="6">
<h1 class="plp_brand_title">abc</h1>
<img class="hero_discription_container_left" src="/qw.png" alt="123" width="500" height="6">
<h1 class="plp_brand_title">ADIDAS BY PHARRELL WILLIAMS</h1>
<img class="hero_discription_container_left" src="/qw.png" alt="123" width="500" height="6">
<h1 class="someotherclass">Not me</h1>
<img class="hero_discription_container_left" src="/qw.png" alt="123" width="500" height="6">
<h1 class="otherclass">remove me</h1>
<img class="hero_discription_container_left" src="/qw.png" alt="123" width="500" height="6">

下面是另一個使用element.nextElementSibling的方法,它允許我們逐個遍歷元素。

window.addEventListener('load', () => {
  let removeImagesAfter = ["ADIDAS BY PHARRELL WILLIAMS"].map(e=>e.toLowerCase()); // for sake of comparisons, we convert these to lower case 
  document.querySelectorAll("h1.plp_brand_title").forEach(h1 => {
    if (removeImagesAfter.includes(h1.innerText.trim().toLowerCase())) {
      let sibling = h1.nextElementSibling
      while (sibling && sibling.tagName !== 'IMG') {
        sibling = sibling.nextElementSibling
      }
      sibling.style.display = "none";
    } else {
      h1.nextElementSibling.style.display = "block";
    }
  })

})
<h1 class="plp_brand_title">abc</h1>
<img class="hero_discription_container_left" src="/qw.png" alt="123" width="500" height="6">
<h1 class="plp_brand_title">ADIDAS BY PHARRELL WILLIAMS</h1>
<img class="hero_discription_container_left" src="/qw.png" alt="123" width="500" height="6">

主站蜘蛛池模板: 亚洲综合av一区二区三区| 国产精品伦一区二区三级视频| 国产激情一区二区三区 | 性色av闺蜜一区二区三区| 国产一区二区视频免费| 亚洲日本一区二区| 亚洲一区二区三区在线播放| 中文字幕一区二区三区久久网站| 国产产一区二区三区久久毛片国语 | 国产高清精品一区| 精品午夜福利无人区乱码一区| 丝袜人妻一区二区三区| 国产精品成人国产乱一区| 国产精品主播一区二区| AV天堂午夜精品一区| 日韩少妇无码一区二区三区| 日本高清天码一区在线播放| 国产精品制服丝袜一区| 人妻无码第一区二区三区| 久久99国产精品一区二区| 国产一区二区三区91| 国产精品va无码一区二区| 久久婷婷色综合一区二区| 成人一区二区免费视频| 中文字幕一区二区在线播放| 国产激情一区二区三区 | aⅴ一区二区三区无卡无码| 久久久久人妻精品一区三寸蜜桃| 国产成人无码精品一区不卡| 国产一区中文字幕在线观看 | 日本免费精品一区二区三区| 日本一区午夜爱爱| 一夲道无码人妻精品一区二区 | 无码人妻一区二区三区免费 | 国产成人无码一区二区在线观看| 久久精品国产一区二区电影| 色久综合网精品一区二区| 亚洲变态另类一区二区三区| 国产一区在线视频观看| 少妇人妻偷人精品一区二区| 国产激情无码一区二区|