html 未捕获的类型错误:无法读取未定义的属性(阅读“parentNode”)

0tdrvxhp  于 2023-03-21  发布在  其他
关注(0)|答案(1)|浏览(209)

我已经添加了以下脚本到我的网页,在那里可以发生新闻通讯注册操作。我试图显示一些自定义内容,一旦用户订阅。

<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”类之前,当订阅按钮被单击时,脚本正在加载。任何见解都将非常感谢!

omjgkv6w

omjgkv6w1#

getElementsByClassName -给出了一个数组,因此您需要从该数组中获取项并使用它。

var news = document.getElementsByClassName("et_pb_newsletter_success")[0];

相关问题