jquery动态创建表格

效果图如下(输入姓名和密码在下方表格中显示)
在这里插入图片描述

代码如下

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="./lib/jquery-3.3.1.min.js"></script>
</head>
<body>
    <div>
        姓名:<input type="text" class="name">
        密码:<input type="text" class="password">
    </div>
    <button>提交</button>
    <table border="1">
        <tr>
            <td>姓名</td>
            <td>密码</td>
        </tr>
    </table>
</body>
<script>
    $(function () {
        $('button').click(function () {
            // console.log(232)
            let name = $(this).siblings("div").find(".name").val()
            // console.log(name)
            let password = $(this).siblings("div").find(".password").val()
            $(this).siblings("div").find(".name").val('')
            $(this).siblings("div").find(".password").val('')
            var $tr = $("tr:first").clone()
            if(name&&password){
                $('table').append($tr)
                $('table tr:last').find('td:first').html(name)
                $('table tr:last').find('td:last').html(password)
            }

        })
    })
</script>
</html>

猜你喜欢

转载自blog.csdn.net/wxz340825/article/details/84633684