我正在尝试制作堆栈的工作模型,其中用户可以选择堆栈 s1 或 s2 并可以执行诸如推送和弹出之类的操作。
<html>
<head>
<title>Stack With Constructor </title>
</head>
<body>
<div>
<p id="p1">
stack
</p>
<input type="checkbox" id="stack1" value="s1">s1
<br>
<input type="checkbox" id="stack2" value="s2">s2
<br>
<textarea id="tdisplay"></textarea>
<textarea id="tpush"></textarea>
<button onclick="doJob()">push</button>
<button onclick="doJob1()">pop</button>
<textarea id="tpop"></textarea>
</div>
<script>
function myfunction() {
if (document.getElementById("stack1").checked == true) {
console.log('myFunction called!');
s1 = new MyStack();
}
if (document.getElementById("stack2").checked == true) {
s2 = new MyStack();
}
}
function push(v) {
if (this.st === 0) {
console.log("Stack Overflow");
} else {
this.st = this.st - 1;
this.stk[this.st] = v;
document.getElementById("tdisplay").value=this.print();
}
}
function pop() {
if (this.st === 10) {
console.log("Stack Underflow");
} else {
var temp = this.stk[this.st];
this.st = this.st + 1;
document.getElementById("tdisplay").value=this.print();
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("tpush").value;
s1.push(x);
document.getElementById("tpush").value="";
}
function doJob1(){
var y=s1.pop();
document.getElementById("tpop").value=y;
}
</script>
</body>
我不知道如何使用复选框方法。这意味着当我选择 s1 复选框时,它应该创建 s1 堆栈,我可以对其进行操作,与 s2 相同。
结果应该是交互式堆栈的正确浏览器功能,因此它可以像以下步骤一样工作:
用户选择s1或s2堆栈
如果他/她选择 s1,那么他们可以执行 s1 的所有操作。
用户也可以在s2之间切换并可以执行所有操作。
我在复选框工作时遇到问题。
我认为单选按钮会让你更幸运。如果 s1 和 s2 是单选按钮组,例如
<input type="radio" name="stack_select" id="stack1" value="s1">s1
<br>
<input type="radio" name="stack_select" id="stack2" value="s2">s2
然后,您可以侦听一个函数何时发生更改,并让其他 ui 组件更新正确的选定堆栈。
对于您的情况,复选框的问题在于它们并不相互排斥,而且您似乎永远不想同时选择 s1 和 s2 。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)