原生js动态添加元素

原生js动态添加元素<!DOCTYPEhtml><htmllang=”en”><head><metacharset=”UTF-8″><metaname=”viewport”content=”width=device-width,initial-scale=1.0″><title>原生js动态添加元素</title><style>.phone{.

大家好,又见面了,我是你们的朋友全栈君。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>原生js动态添加元素</title>
    <style>
        .phone {
            width: 200px;
            height: 30px;
        }
        ul {
            width: 200px;
            border: 1px solid #eee;
            text-align: right;
        }
        ul li{
            list-style: none;
            height: 50px;
            line-height: 50px;
            padding-right: 20px;
            border-right: 2px solid #999;
        }
        ul li:hover {
            color: #f00;
            border-right: 2px solid #f00;
        }
        /* .circle1 {
            width: 492px;
            height: 499px;
            border-radius: 50%;
            background: #f00;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        .circle2 {
            width: 260px;
            height: 434px;
            border-radius: 50%;
            background: #fff;
        } */
    </style>
</head>
<body>
    <div class="container">
        <form action="" id="form">

        </form>
        <ul id="parentUI">
            <li>你好</li>
            <li>关于我们</li>
            <li>帮助中心</li>
        </ul>
    </div>
    <script>
        var formId = document.getElementById("form");
            // 创建元素input
        var inputPhone = document.createElement("input");
            // 设置input的属性
            inputPhone.setAttribute("class","phone");
            inputPhone.type = "text";
            inputPhone.value = "请输入手机号码";
            formId.appendChild(inputPhone);

        // js动态添加li
        var ul = document.getElementById("parentUI");
        var li = document.createElement("li");
            li.setAttribute("class","list");
            li.innerHTML = "联系我们";
            ul.appendChild(li);
    </script>
</body>
</html>

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/152106.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号