繁体   English   中英

如何将函数值存储在变量中并在textarea上显示

[英]how to store function value in variable and display it on textarea

我试图将函数的值存储在变量中,并希望在文本区域t1显示内容。

因此,每当我按下该值时,我都可以在变量s获得打印功能,并可以在文本区域t1显示内容。

<html>
    <head>
        <title>Stack With Constructor </title>
    </head>
    <body>
        <div>Stack </div>
        <div>
            <h1>stack</h1>
            <h2>Stage1.</h2>
            <p id="p1">
                stack
            </p>
            <textarea id="t1"></textarea>
            <button onclick="doJob()">push</button>
            <button onclick="doJob1()">pop</button>
        </div>
        <textarea id="t"></textarea>
        <script>
            function push(v) {
                if (this.st === 0) {
                    console.log("Stack Overflow");
                } else {
                    this.st = this.st - 1;
                    this.stk[this.st] = v;
                }
            }

            function pop() {
                if (this.st === 10) {
                    console.log("Stack Underflow");
                } else {
                    var temp = this.stk[this.st];
                    this.st = this.st + 1;
                    return temp;
                }
            }

            function print() {
                console.log("Printing Stack");
                for (var i = this.st ; i < 10; i++) {
                    console.log(this.stk[i]);
                }
            };

            function MyStack() {
                this.st = 10;
                this.stk = new Array(10);
                this.push = push;
                this.pop = pop;
                this.print = print;
            };

            var s1 = new MyStack();

            function doJob() {
                var x=document.getElementById("t").value;
                s1.push(x);
                var s=s1.print();
                document.getElementById("t1").value=s;
            }
        </script>
    </body>
</html>

我想在文本区域t1显示打印功能,以便它可以作为交互式堆栈使用。

当我尝试增加价值时,我在文本区域中变得undefined

更改您的打印功能,例如

        function print() {
            console.log("Printing Stack");
            var str = "";//empty string
            for (var i = this.st ; i < 10; i++) {
                console.log(this.stk[i]);
                str+=this.stk[i]+'\n';//concatenate the value and a new line
            }
            return str;
        };

DEMO

 <html> <head> <title>Stack With Constructor </title> </head> <body> <div>Stack </div> <div> <h1>stack</h1> <h2>Stage1.</h2> <p id="p1"> stack </p> <textarea id="t1"></textarea> <button onclick="doJob()">push</button> <button onclick="doJob1()">pop</button> </div> <textarea id="t"></textarea> <script> function push(v) { if (this.st === 0) { console.log("Stack Overflow"); } else { this.st = this.st - 1; this.stk[this.st] = v; } } function pop() { if (this.st === 10) { console.log("Stack Underflow"); } else { var temp = this.stk[this.st]; this.st = this.st + 1; return temp; } } function print() { console.log("Printing Stack"); var str = "";//empty string for (var i = this.st ; i < 10; i++) { console.log(this.stk[i]); str+=this.stk[i]+'\\n';//concatenate the value and a new line } return str; }; function MyStack() { this.st = 10; this.stk = new Array(10); this.push = push; this.pop = pop; this.print = print; }; var s1 = new MyStack(); function doJob() { var x=document.getElementById("t").value; s1.push(x); var s=s1.print(); document.getElementById("t1").value=s; } function doJob1(){ s1.pop(); var s=s1.print(); document.getElementById("t1").value=s; } </script> </body> 

暂无
暂无

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

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