JS中如何實現每點擊一次按鈕,顯示一條信息


<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<script type="text/javascript">
var clickNumber = 0;
function Student(name, age) {
this.name = name;
this.age = age;
}
Student.prototype.showInfo = function () {
var pObj = document.createElement("p");
var textObj = document.createTextNode(this.name + " | " + this.age);
pObj.appendChild(textObj);
var firstP = document.getElementById("title");
/* IE9 之后的才兼容 var firstDiv = firstP.nextElementSibling;
firstDiv.appendChild(pObj);*/
firstP.nextSibling.nextSibling.appendChild(pObj); //各種瀏覽器都兼容
/*alert(firstDiv);*/

}

var count = 0;
var stu1 = new Student("張三", 18);
var stu2 = new Student("李四", 19);
var student = [stu1, stu2];

function show() {
if (count < student.length) {
student[count].showInfo();+
count++;
}
}

</script>
</head>
<body>
<p id="title">姓名&nbsp;&nbsp;|&nbsp;&nbsp;年齡</p>
<div>

</div>
<input type="button" id = "but" value="顯示下一條數據" onclick="show()"/>
</body>

</html>

 

效果展示:

未點擊:

點擊一次:

點擊第二次:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM