码迷,mamicode.com
首页 > Web开发 > 详细

JS中如何实现每点击一次按钮,显示一条信息

时间:2015-08-12 21:09:06      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:

<!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>

 

效果展示:

未点击:

技术分享

点击一次:

技术分享

点击第二次:

技术分享

JS中如何实现每点击一次按钮,显示一条信息

标签:

原文地址:http://www.cnblogs.com/LisaY/p/4725406.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!