zoukankan      html  css  js  c++  java
  • 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>

    效果展示:

    未点击:

    点击一次:

    点击第二次:

  • 相关阅读:
    面向对象
    用JS添加和删除class类名
    偶然
    js-cookie的用法
    eleemnt-ui修改主题颜色
    router.go,router.push,router.replace的区别
    vue生产环境清除console.log
    特别关心
    echart
    20182330魏冰妍_预备作业
  • 原文地址:https://www.cnblogs.com/LisaY/p/4725406.html
Copyright © 2011-2022 走看看