Javascript 未捕获类型错误:无法读取属性';setAttribute';空值(控制台错误)

Javascript 未捕获类型错误:无法读取属性';setAttribute';空值(控制台错误),javascript,parallax,Javascript,Parallax,我试图创建一个基本的视差背景,代码工作,但我有这个控制台错误,我无法摆脱 未捕获的TypeError:无法读取null的属性“setAttribute” 下面是一个指向codepenn的链接,以获取一个工作示例: HTML: JS: 我真的只是想找到一种方法来清除控制台错误 提前感谢您提供有关如何处理此问题的任何帮助或提示。尝试使用:paraId&¶Id.setAttribute window.onload = function() { const parallax = (id

我试图创建一个基本的视差背景,代码工作,但我有这个控制台错误,我无法摆脱

未捕获的TypeError:无法读取null的属性“setAttribute”

下面是一个指向codepenn的链接,以获取一个工作示例:

HTML:

JS:

我真的只是想找到一种方法来清除控制台错误


提前感谢您提供有关如何处理此问题的任何帮助或提示。

尝试使用:paraId&¶Id.setAttribute

window.onload = function() {

   const parallax = (id, modifier) => {
   const paraId = document.querySelector(id);

       paraId && paraId.setAttribute('style', 'background-repeat: no-repeat; background-attachment: fixed; background-size: cover; background-position: center center; transition-property: background-position;');

       const sp = () => {
           const x = paraId.getBoundingClientRect().top / modifier;
           const y = Math.round(x * 100) / 100;
           paraId.style.backgroundPosition = `center ${  y  }px`;
       };
       sp();
       window.addEventListener("scroll", (e) => {
           sp();
       });
   };

   parallax("#parallax-1", 8);
};

尝试使用:paraId&¶Id.setAttribute

window.onload = function() {

   const parallax = (id, modifier) => {
   const paraId = document.querySelector(id);

       paraId && paraId.setAttribute('style', 'background-repeat: no-repeat; background-attachment: fixed; background-size: cover; background-position: center center; transition-property: background-position;');

       const sp = () => {
           const x = paraId.getBoundingClientRect().top / modifier;
           const y = Math.round(x * 100) / 100;
           paraId.style.backgroundPosition = `center ${  y  }px`;
       };
       sp();
       window.addEventListener("scroll", (e) => {
           sp();
       });
   };

   parallax("#parallax-1", 8);
};

你的浏览器是什么?我在chrome上测试过,它似乎工作得很好,你使用的是什么浏览器?所以在你的代码中它找不到元素,所以你需要找出它找不到元素的原因。你的浏览器是什么?我在chrome上测试过,它似乎工作得很好,您使用的浏览器是什么?因此在代码中它找不到元素,因此您需要找出它找不到元素的原因。
window.onload = function() {

   const parallax = (id, modifier) => {
   const paraId = document.querySelector(id);

       paraId.setAttribute('style', 'background-repeat: no-repeat; background-attachment: fixed; background-size: cover; background-position: center center; transition-property: background-position;');

       const sp = () => {
           const x = paraId.getBoundingClientRect().top / modifier;
           const y = Math.round(x * 100) / 100;
           paraId.style.backgroundPosition = `center ${  y  }px`;
       };
       sp();
       window.addEventListener("scroll", (e) => {
           sp();
       });
   };

   parallax("#parallax-1", 8);
};
window.onload = function() {

   const parallax = (id, modifier) => {
   const paraId = document.querySelector(id);

       paraId && paraId.setAttribute('style', 'background-repeat: no-repeat; background-attachment: fixed; background-size: cover; background-position: center center; transition-property: background-position;');

       const sp = () => {
           const x = paraId.getBoundingClientRect().top / modifier;
           const y = Math.round(x * 100) / 100;
           paraId.style.backgroundPosition = `center ${  y  }px`;
       };
       sp();
       window.addEventListener("scroll", (e) => {
           sp();
       });
   };

   parallax("#parallax-1", 8);
};