javascript - Shopify 变体产品 - 未捕获的类型错误:无法读取 null 的属性“样式”
问题描述
我在 Shopify 网站上工作,并试图关闭 4 包的订阅选项。
我有这段代码,但我遇到了一个我不熟悉的错误。希望有人能指出我正确的方向来解决。谢谢!
网站: https ://crispandcrude.myshopify.com/products/mellow-mule (密码:susahx)
错误: “未捕获的 TypeError:无法读取 null 的属性 'style'”
代码:
/* ======================================
DISABLE 4 Pack Subscriptions
========================================= */
window.addEventListener('DOMContentLoaded', (event) => {
let noSub = '4 Pack'
let variants = document.querySelectorAll('[data-value]');
let rechargeForm = document.querySelector('.rc_container')
let initialCheckedValue = document.querySelector('.swatch input:checked')
// On variant change
variants.forEach(variant =>
variant.addEventListener('click', e=> {
if (e.target.value == noSub) {
// Hide RC widget and click first/one time option
rechargeForm.style.display = 'none'
document.querySelector('.rc_widget__option--onetime input').click();
} else {
rechargeForm.style.display = 'block'
}
})
)
// On first load
if (initialCheckedValue.value == noSub) {
// Hide RC widget and click first/one time option
rechargeForm.style.display = 'none'
document.querySelector('.rc_widget__option--onetime input').click();
} else {
rechargeForm.style.display = 'block'
}
});
解决方案
用 document.getElementById("DivID") 替换您的 chargeForm。注意:根据您的需要更新 id。
<!DOCTYPE html>
<html>
<head>
<style>
#DivID{
width: 250px;
height: 250px;
background-color: blue;
}
</style>
</head>
<body>
<button onclick="clickFunction()">Click here to hide</button>
<div id="DivID">
This section will hide after click
</div>
<script>
function clickFunction() {
document.getElementById("DivID").style.display = "none";
}
</script>
</body>
</html>
推荐阅读
- python - 帕累托优化前沿
- java - org.jboss.weld.exceptions.UnsatisfiedResolutionException WELD-001334 带有限定符 @Any @MyAnnotation 的类型 MyInterface 的依赖关系不满足
- excel - Excel 2016:如果在工作表 2 中找不到任何软件,则工作表 1 中的颜色 cel
- javascript - 尽管已安装,但“同时:找不到命令”
- quarkus - 如何将 Quarkus 日志配置为仅跟踪我的“my.package”类别?
- rust - 使用 Cargo 为使用相同源目录的多个平台构建的最佳方法是什么?
- java - 如何在选项窗格上显示消息?
- c - 如何找到 PIE 二进制文件的负载重定位?
- javascript - 当 DOM 过于相似时,React SSR 和水合物出现问题
- python - 打开重定向的链接