2016-04-03 65 views
0

正如它在标题中所说,我无法显示我的生活 另一个div。请检查并启发我为什么无法获得“id = output1”来显示?无法显示document.getElementById(“output1”)中的“output1”。innerHTML =“L1:”L1.DisplayAll();

以下是HTML:

<input type="button" value="Insert Queue Length" onClick="InitializeQueue();" /> 
    <br/> 
    <div id="output"> 
    <br/> 
    <div id="output1"> 
    <br/> 

以下是函数和方法的JavaScript排队

function Queue(){ 
     var count = 0; 
     var head = null; 
     var tail = null; 
     this.GetCount = function(){ 
      return count; 
     } 

    this.Enqueue = function (data) { 
     //fill queue 
     var node = { 
      data: data, 

      next: head 
     }; 
     if (head === null) { 
      tail = node; 
     } 
     head = node; 
     count++; 
    } 

    this.Dequeue = function() { 
//if queue is empty, returns null 
     if (count === 0) { 
      return; 
     } 
     else { 
      var current = head; 
      var previous = null; 
      while (current.next) { 
       previous = current; 
       current = current.next; 
      } 
      if (count > 1) { 
       previous.next = null; 
       tail = previous; 
      } 
      else { 
       head = null; 
       tail = null; 
      } 

      count--; 
     } 
    } 

    this.DisplayAll = function() { 
     if (head === null) { 
      return null; 
     } 
     else { 
      var arr = new Array(); 
      var current = head; 

      for (var i = 0; i < count; i++) { 
       arr[i] = current.data; 
       current = current.next; 
      } 
     arr = arr.reverse(); 
      return arr; 
     } 
    } 

     } 

这将创建一个链接列表和添加节点

var L1 = new Queue(); 

    function InitializeQueue() { 
     var iteration = 0; 
     var y = parseInt(document.getElementById("QueueLength").value); 
     for(i = 2; i <= y; i++){ 
      L1.Enqueue(i); 
      document.getElementById("output").innerHTML = "L1: "+  L1.DisplayAll(); 
      } 
      L1.Dequeue(); 
      //For some reason this wont display, but the dequeue seems to work properly 
      document.getElementById("output1").innerHTML = "L1: "+ L1.DisplayAll(); 


    } 
    </script> 

回答

0

你确定你不需要关闭div吗?如果这不是问题,请用一个工作示例创建一个小提琴。

<input type="button" value="Insert Queue Length" onClick="InitializeQueue();" /> 
<br/> 
<div id="output"></div> 
<br/> 
<div id="output1"></div> 
<br/> 
+0

好吧,这就是我得到这个代码5个小时,并卡住了20分钟和哭狼,抱歉花任何人的时间。谢谢Niels! –