我已经将以下脚本添加到我的网页中,当用户进行newsletter注册时会触发。我想在用户订阅后显示一些自定义内容。
<script>
function insertAfter(referenceNode, newNode) {
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}
var news = document.getElementsByClassName("et_pb_newsletter_success")[0].selectedIndex;
var text = document.createTextNode("Thank you for joining our SOS Family! We are very grateful to have you with us. Thanks to compassionate people like you, SOS is meeting needs and improving the lives of our friends and neighbours in District 69. You can expect valuable emails from us on a regular basis, and you can unsubscribe at any time.");
var learn = document.createTextNode("Learn more about SOS.")
insertAfter(news,text);
insertAfter(text,learn);
var img = document.createElement("img");
img.src = "https://sosd69.com/wp-content/uploads/newsletter_success.jpeg";
insertAfter(learn,img);
let btn = document.createElement("button");
btn.innerHTML = "Watch Video";
btn.onclick = function () {
window.location='https://www.youtube.com/watch?v=OcYyhazCiHs';
};
btn.classList.add("et_pb_button_0");
insertAfter(img,btn);
</script>
我收到了以下控制台错误:
Uncaught TypeError: Cannot read properties of undefined (reading 'parentNode')
at insertAfter ((index):666)
at (index):672
我创建的insertAfter函数是否存在错误?也许问题是脚本在"et_pb_newsletter_success"类被创建之前加载,当用户点击订阅按钮时。非常感谢您提供任何见解!
gEBCN
问题之外,还存在一个逻辑错误,新创建的文本节点尚未插入到任何元素中,因此它们无法具有父节点。 - Teemu