简体   繁体   中英

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.

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. 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

I know it means that there is no object with that Id, but I set an object to have that Id right before. 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.

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

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

DEMO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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