动态创建元素(createElement())

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_32077521/article/details/97615650
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
	<meta charset="utf-8" />
</head>
<body>
    <style type="text/css">
        #div1 {
        width:300px;
        height:300px;
        border:1px solid red;
        }
    </style>
    <input type="button" id="btn1" value="动态创建一个元素"/>
    <div id="div1"></div>
    <script type="text/javascript">
        window.onload = function () {
            document.getElementById('btn1').onclick = function () {
                var alink = document.createElement('a');
                alink.href = 'http://www.baidu.com';
                alink.target = '_blank';
                alink.title = '百度一下,你就知道!';
                alink.innerText = '百度';
                document.getElementById('div1').appendChild(alink);
                var txtInput = document.createElement('input');
                txtInput.type = 'text';
                document.getElementById('div1').appendChild(txtInput);
            };
        };
    </script>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/qq_32077521/article/details/97615650