JavaScript 11.03
1.1 什么是DOM 操作
文档Document对象Object模型Model
通过js中的内置对象document对网页的内容进行增删改查。
增:类似与 发表说说
1.2 增
A 传统方式
<input id="bar" /><button onclick="haha()">发表</button>
<p id="foo">
您还没有说说。
</p>
<script type="text/javascript">
function haha(){
//1. 动态创建
var a= document.createElement("h1");
//2. 优化
a.innerHTML = bar.value;
//3. 动态添加
foo.appendChild(a);
}
</script>
B 模板方式
将我们要动态生成的内容提前写好 , 当我们需要发布的时候 , 我们直接将 模板动态放到页面中
<style type="text/css">
#aaa {
border: 2px solid red;
padding: 10px;
}
</style>
</head>
<body>
<input id="bar" /><button onclick="haha()">发表</button>
<p id="foo"> </p>
<script type="text/template" id="temp">
<div id="aaa">
<img src="img/pie_0.jpg" width="50" alt="">
<span>张三</span>