JavaScript创建按钮,实现数字自加1!!

大致步骤:

1、写一个p标签,指定一个id选择器,输入数字!

2、写一个input标签,指定type属性的属性值为button,创建一个按钮,加入onclick事件!

3、为p标签和input标签指定相关的CSS样式(可以省略)

4、用js创建一个自加的函数,在函数中用document对象的getElementById()方法,选中p标签。

5、通过innerHTML获取p标签的内容,实现自加!!

 

实现代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>自加</title>
    <style>
            body {
                text-align: center;
            }
            p,#ipt,#btn {
                font-size: 50px;
            }
            #ipt {
                width: 100px;
                margin-bottom: 10px;
            }
            #ipt,#btn {
                height: 100px;
                padding: 0px 20px;
            }
        </style>
        <script>
           function numPlus() {
                var p = document.getElementById('num');
                p.innerHTML++;
           }
        </script>
</head>
<body>
        <p id="num">1</p>
        <input type="button" id="btn" value="click +1" onclick="numPlus()" />
</body>
</html>

效果演示:

 

0

posted @ 2019-03-03 17:15  Mr_Valentine  阅读(6675)  评论(0编辑  收藏  举报