例:应用JavaScript编写留言的功能,在文本中输入文字提交后,在下方进行显示。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Javascript编写留言功能</title>
</head>
<style type="text/css">
.mainBox{
width: 500px;
margin: 0 auto;
}
.textBox{
width: 500px;
height: 200px;
word-wrap: break-word;
}
.submitBox{
margin-top: 5px;
float: right;
}
.submit{
width: 100px;
}
.massage{
width: 500px;
margin: 80px;
}
li{
list-style: none;
height: 40px;
line-height: 40px;
font-size: 18px;
border-bottom: 1px solid rgb(185, 182, 182);
font-family: 黑体;
}
</style>
<body>
<div id="app">
<div class="mainBox">
<form action="">
<div >
<input class="textBox" v-model="massage"></input>
</div>
<div class="submitBox">
<input type="button" value="提交" class="submit" @click="createDom">
</div>
</form>
</div>
<div class="massage">
<ul id="textList">
<li>Hello</li>
<li>你好,西南石油大学</li>
</ul>
</div>
</div>
<script src="vue.js"></script>
<script type="text/javascript">
const app = new Vue({
el: '#app',
data: {
massage: 'Hello SWPU',
},
methods: {
createDom(){
var li = document.createElement("li");
li.innerText = this.massage;
document.getElementById("textList").appendChild(li);
this.massage='';
}
}
})
</script>
</body>
</html>