<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<input id="queue-input" type="text">
<p id="queue-cont">队列内容:apple->pear</p>
<button id="in-btn" onclick="aa()">入队</button>
<button id="out-btn" onclick="bb()">出队</button>
<button id="font-btn" onclick="cc()">打印队头元素内容</button>
<button id="empty-btn" onclick="dd()">判断队列是否为空</button>
<script>
var queue = ["apple", "pear"];
function print(){
var str=document.getElementById('queue-cont');
str.innerHTML=queue;
}
function aa(){
var text=document.getElementById('queue-input');
queue.push(text.value);
console.log("添加元素成功"+text.value);
print();
}
function bb(){
console.log("出队成功"+queue.shift());
print();
}
function cc(){
console.log(queue[0]);
print();
}
function dd(){
if(queue[0]!=null){
console.log("队列非空");
}
else{
console.log("队列为空");
}
}
</script>
</body>
</html>
1.数组可以直接赋值给元素的内容
2.shift()方法,删除数组第一个元素且返回原数组第一个元素