简体   繁体   English

JavaScript Chrome似乎不支持通过setAttributeNode添加ID

[英]JavaScript Chrome does not seem to support adding an id through setAttributeNode

So I am working on a JavaScript searcher for my website, and It's been up for a few days, when I realized that it is not compatible with Chrome, so i checked a few other browsers and realized it works in Firefox and IE9, but does not work in Chrome or Safari. 所以我正在为我的网站开发一个JavaScript搜索器,它已经用了几天,当我意识到它与Chrome不兼容时,所以我检查了一些其他浏览器,并意识到它适用于Firefox和IE9,但是无法在Chrome或Safari中使用。

I've been pouring over the debugger in Chrome and Firefox side by side for hours, and cannot for the life of me figure out what the problem is. 我已经将Chrome和Firefox中的调试器并排倾倒了几个小时,并且不能为我的生活找出问题所在。 It keeps giving me an error. 它一直给我一个错误。 I know exactly what the error means, and why it is giving me the error, but I cannot figure out what is causing it. 我确切地知道错误意味着什么,以及为什么它给我错误,但我无法弄清楚是什么导致了它。

This is the error: Uncaught TypeError: Cannot set property 'innerHTML' of null 这是错误:Uncaught TypeError:无法设置null的属性'innerHTML'

I know it means that there is no object with that Id, but I set an object to have that Id right before. 我知道这意味着没有该Id的对象,但是我设置了一个对象以便拥有该ID。 In Firefox's debugger at that line of code it says that the Id has been set, but at the same line in Chrome's debugger it says it has not. 在该代码行的Firefox调试器中,它表示已经设置了Id,但在Chrome调试器的同一行中,它表示它没有。

Here is an excerpt of the code: 以下是代码的摘录:

  arrayFinal[arrayln2]="end";
  var displayNumber=0;
  while(arrayFinal[displayNumber].charAt(0) != "e"){
    var boxPath="camper_htmls/"+arrayFinal[displayNumber]+".txt";
    boxhttp = new XMLHttpRequest();
    boxhttp.open("GET",boxPath,false);
    boxhttp.send(null);
    var boxHTML = boxhttp.responseText;
    var setDivId=document.createAttribute("Id");
    setDivId.value=("div_"+displayNumber);
    var node = document.createElement("div");
    node.setAttributeNode(setDivId);
    document.getElementById("resultContainer").appendChild(node);
    var divIdNumber = ("div_"+displayNumber);
    document.getElementById(divIdNumber).innerHTML=boxHTML;
    displayNumber++;
  }

although a version of this code is being used on almost every page of the site, and none of them work. 虽然该代码的几乎每个页面都使用了此代码的一个版本,但它们都不起作用。

Use id instead of Id as in 使用id而不是Id

var setDivId=document.createAttribute("id");

DEMO DEMO

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM