简体   繁体   English

如何修复“Uncaught TypeError:无法设置属性'innerHTML'为null”

[英]How to fix “Uncaught TypeError: Cannot set property 'innerHTML' of null”

My code compares two arrays and shows items that are in both arrays. 我的代码比较两个数组并显示两个数组中的项目。 I can get this to successfully print to the console to transform a paragraph is proving more challenging. 我可以得到这个成功打印到控制台转换一个段落证明更具挑战性。

Uncaught TypeError: Cannot set property 'innerHTML' of null at compareArrays (pen.js:25) at HTMLButtonElement.onclick (index.html?key=iFrameKey-ae252606-6389-a594-b844-2cecca064c7d:20) compareArrays @ pen.js:25 onclick @ index.html?key=iFrameKey-ae252606-6389-a594-b844-2cecca064c7d:20 未捕获的TypeError:无法在compareBrtonElement.onclick(index.html?key = iFrameKey-ae252606-6389-a594-b844-2cecca064c7d:20)compareArrays @ pen.js的compareArrays(pen.js:25)中设置null的属性'innerHTML' :25 onclick @ index.html?key = iFrameKey-ae252606-6389-a594-b844-2cecca064c7d:20

I understand I have to use the innerHTML method but unsure what my errors mean 我知道我必须使用innerHTML方法,但不确定我的错误是什么意思

<button id="search" onclick="compareArrays()" required>Search</button>
<p id="results"></p>
function compareArrays () {
  // Interate the first array
  for (var i = 0; i < restaurantsOne.length; i++) {
    // Iterate the second array 
    for (var j = 0; j < restaurantsTwo.length; j++){
      // Compare the two arrays
      if (restaurantsOne[i] == restaurantsTwo[j]){
        // console.log(restaurantsOne[i]);
        // Show results in paragraph
        document.getElementById("#results").innerHTML = restaurantsOne[i] 

      }
    }
  }
 }

Ultimate I would like each item, restaurantsOne[i], that is in both arrays to print to a new paragraph. 终极我希望每个项目,restaurantOne [i],在两个阵列中打印到一个新的段落。

I think that's because of your document.getElementById. 我认为那是因为你的document.getElementById。

It should be like this 它应该是这样的

    document.getElementById("results").innerHTML = restaurantsOne[i] 

(no #) (没有#)

The # can be used with the document.querySelector method #可以与document.querySelector方法一起使用

The DOM doesn't exist at the time your script is running, so no element is found. 脚本运行时DOM不存在,因此找不到任何元素。

Either move your script to the bottom of your HTML page: 将脚本移动到HTML页面的底部:

  <script>...</script>
</body>

Or wrap it all in an onload listener: 或者将它全部包装在onload侦听器中:

window.onload = function() {
  // All your code
};

Also note that your ID most likely doesn't contain a # - you probably don't have or want this: 另请注意,您的ID很可能不包含# - 您可能没有或想要这样:

<p id="#results"></p>

Either remove it: 删除它:

document.getElementById("results")

Or use querySelector : 或者使用querySelector

document.querySelector("#results")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的TypeError:无法将属性&#39;innerHTML&#39;设置为null - Uncaught TypeError: Cannot set property 'innerHTML' of null 未捕获的Typeerror:无法将属性innerHTML设置为null - Uncaught Typeerror: Cannot set property innerHTML of null 未捕获的TypeError:无法将属性&#39;innerHTML&#39;设置为null - Uncaught TypeError: Cannot set property 'innerHTML' of null 未捕获的TypeError:无法将属性&#39;innerHTML&#39;设置为null - Uncaught TypeError: Cannot set property 'innerHTML' of null 未捕获的TypeError:无法设置null的属性&#39;innerHTML&#39; - Uncaught TypeError: Cannot set property 'innerHTML' of null 如何修复未捕获的类型错误:无法在 wordpress 的 javascript 中设置 null 的属性“innerHTML”? - How to fix Uncaught TypeError: Cannot set property 'innerHTML' of null in javascript in wordpress? 如何修复“未捕获的 TypeError:无法设置属性 'onchange' of null”? - How to fix "Uncaught TypeError: Cannot set property 'onchange' of null"? 未捕获的TypeError:无法在phonegap中将属性&#39;innerHTML&#39;设置为null - Uncaught TypeError: Cannot set property 'innerHTML' of null in phonegap 未捕获的TypeError:无法使用for循环将属性&#39;innerHTML&#39;设置为null - Uncaught TypeError: Cannot set property 'innerHTML' of null using a for loop 未捕获的TypeError:无法在第34行中将属性&#39;innerHTML&#39;设置为null - Uncaught TypeError: Cannot set property 'innerHTML' of null in line 34
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM