AJAX DOM添加删除元素

来源:互联网 发布:linux登录oracle数据库 编辑:程序博客网 时间:2024/04/28 06:44

JS DOM创建元素

DOM:文本对象模型
document 对象

1.创建新的HTML元素

创建该元素
找一个已经存在的元素,追加新元素进去

<!DOCTYPE html><html><head>    <title></title>    <meta charset="utf-8"></head><body><script type="text/javascript">function pa(){var p=document.createElement('p');var node=document.createTextNode("草");p.appendChild(node);var a=document.getElementById('div1');a.appendChild(p);}</script><div id="div1"><p>你吗</p></div><input type="button" name="" onclick="pa()"></body></html>

2.删除已有元素

<!DOCTYPE html><html><head>    <title></title>    <meta charset="utf-8"></head><body><script type="text/javascript">function pa(){var div1=document.getElementById("div1");var p1=document.getElementById("pp");div1.removeChild(p1);}</script><div id="div1"><p id="pp">你吗</p></div><input type="button" name="" onclick="pa()"></body></html>
0 0
原创粉丝点击