无法从document.getElementById(“output1”)显示“output1”.innerHTML =“L1:”L1.DisplayAll();

时间:2016-04-03 23:16:27

标签: javascript

正如标题中所说,我无法展示生命中的另一个div 我的。请查看并告诉我为什么我无法显示“id = output1”?

以下是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>

1 个答案:

答案 0 :(得分:0)

你确定你不需要关闭div吗?如果这不是问题,请创建一个小例子。

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