使用纯JavaScript,请根据Swatch活动类和数据选择选项隐藏一个元素?
我正在尝试显示一个文件上传按钮仅适用于“测试产品”页面中的一个名为“自定义徽标”的样本选项在这里。
下面的我的代码不完整,因为我发现很难读取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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该对你有用
This should work for you