使用纯JavaScript,请根据Swatch活动类和数据选择选项隐藏一个元素?

发布于 2025-02-13 19:30:14 字数 1341 浏览 1 评论 0原文

我正在尝试显示一个文件上传按钮仅适用于“测试产品”页面中的一个名为“自定义徽标”的样本选项在这里

下面的我的代码不完整,因为我发现很难读取data-swatch-option属性的值,并查找span是否有一个名为swatch的类 - Active指示选择了相关的色板。

<span data-swatch-option="Custom Logo" data-swatch-index="6947439312956-0" data-booster-initd="true" class="swatch--active">Custom Logo</span>
  // Show File Upload button only if Custom Logo variant is clicked/active.
  const el = document.querySelectorAll('span');
  window.onload = function() {
    setTimeout(function() {
      console.log(JSON.stringify(input));
      document.getElementById('hulkapps_option_list_6947439312956').style.display = 'none';
      for (var i = 0; i < el.length; i++) {
        const current = el[i];
        el[i].addEventListener("click", function (e) {
          if(current.dataset.swatchOption == 'Custom Logo'){
            document.getElementById('hulkapps_option_list_6947439312956').style.display = 'block';
          }else{
            document.getElementById('hulkapps_option_list_6947439312956').style.display = 'none';
          }
        });
      }
    }, 2000);
  };

I'm trying to show a file upload button only for one of the swatch options named 'Custom Logo' in the test product page here.

My code below is incomplete because I find it difficult to read the value of the data-swatch-option attribute and to find if the span has a class named swatch--active that indicates that the swatch in question is selected.

<span data-swatch-option="Custom Logo" data-swatch-index="6947439312956-0" data-booster-initd="true" class="swatch--active">Custom Logo</span>
  // Show File Upload button only if Custom Logo variant is clicked/active.
  const el = document.querySelectorAll('span');
  window.onload = function() {
    setTimeout(function() {
      console.log(JSON.stringify(input));
      document.getElementById('hulkapps_option_list_6947439312956').style.display = 'none';
      for (var i = 0; i < el.length; i++) {
        const current = el[i];
        el[i].addEventListener("click", function (e) {
          if(current.dataset.swatchOption == 'Custom Logo'){
            document.getElementById('hulkapps_option_list_6947439312956').style.display = 'block';
          }else{
            document.getElementById('hulkapps_option_list_6947439312956').style.display = 'none';
          }
        });
      }
    }, 2000);
  };

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

童话里做英雄 2025-02-20 19:30:14

这应该对你有用

const el =  document.querySelectorAll('.type')
const input = document.getElementsByClassName('uploader')

input[0].style.display = 'none'

for (var i = 0; i < el.length; i++) {
  const current = el[i]
    el[i].addEventListener("click", function (e) {
    if(current.dataset.swatchOption == 'Custom Logo'){
      input[0].style.display = 'block'
     }else{
      input[0].style.display = 'none'
     }
      
    });
};
span{
border: 1px solid black;
padding: 10px;
}

input{
margin-top: 50px
}
<div class="swatches__holder">
<span class="type" data-swatch-option="Custom Logo" data-swatch-index="6947439312956-0" data-booster-initd="true">Custom Logo</span>
<span class="type" data-swatch-option="Design 1" data-swatch-index="6947439312956-0" data-booster-initd="true" class="swatch--active">Design 1</span>
 <span class="type" data-swatch-option="Design 2" data-swatch-index="6947439312956-0" data-booster-initd="true">Design 2</span>
<span class="type" data-swatch-option="Design 3" data-swatch-index="6947439312956-0" data-booster-initd="true">Design 3</span>
<span class="type" data-swatch-option="Design 4" data-swatch-index="6947439312956-0" data-booster-initd="true">Design 4</span>
<span class="type" data-swatch-option="Design 5" data-swatch-index="6947439312956-0" data-booster-initd="true">Design 5</span>
<span class="type" data-swatch-option="Design 6" data-swatch-index="6947439312956-0" data-booster-initd="true">Design 6</span>
 </div>
 
 <div class="uploader"><input type="file"></div>

This should work for you

const el =  document.querySelectorAll('.type')
const input = document.getElementsByClassName('uploader')

input[0].style.display = 'none'

for (var i = 0; i < el.length; i++) {
  const current = el[i]
    el[i].addEventListener("click", function (e) {
    if(current.dataset.swatchOption == 'Custom Logo'){
      input[0].style.display = 'block'
     }else{
      input[0].style.display = 'none'
     }
      
    });
};
span{
border: 1px solid black;
padding: 10px;
}

input{
margin-top: 50px
}
<div class="swatches__holder">
<span class="type" data-swatch-option="Custom Logo" data-swatch-index="6947439312956-0" data-booster-initd="true">Custom Logo</span>
<span class="type" data-swatch-option="Design 1" data-swatch-index="6947439312956-0" data-booster-initd="true" class="swatch--active">Design 1</span>
 <span class="type" data-swatch-option="Design 2" data-swatch-index="6947439312956-0" data-booster-initd="true">Design 2</span>
<span class="type" data-swatch-option="Design 3" data-swatch-index="6947439312956-0" data-booster-initd="true">Design 3</span>
<span class="type" data-swatch-option="Design 4" data-swatch-index="6947439312956-0" data-booster-initd="true">Design 4</span>
<span class="type" data-swatch-option="Design 5" data-swatch-index="6947439312956-0" data-booster-initd="true">Design 5</span>
<span class="type" data-swatch-option="Design 6" data-swatch-index="6947439312956-0" data-booster-initd="true">Design 6</span>
 </div>
 
 <div class="uploader"><input type="file"></div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文