JavaScript第三种创建元素的方式

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        div {
            width: 300px;
            height: 200px;
            border: 2px solid pink;
        }
    </style>
</head>
<body>
<input type="button" value="添加一个p" id="bt"/>
<div id="dv"></div>
<script src="common.js"></script>
<script>
    //第三种方式创建元素
    //创建元素
    //document.creatElement("标签名字"); 对象
    //把元素追加到父级元素中
    //点击按钮在div中创建一个p
    my$("bt").onclick = function () {
      //创建元素
        var pObj = document.createElement("p");
        setInnerText(pObj, "这是一个p");
        //把创建的子元素追加到父级元素中
        my$("dv").appendChild(pObj);
    };
</script>
</body>
</html>

猜你喜欢

转载自www.cnblogs.com/cuilichao/p/9382928.html