1、编程:实例演示 id,class,标签和css选择器获取元素的方法
<style type="text/css">
.classExample {
width: 90%;
height:80px;
margin: auto;
}
</style>
<script>
let btn=document.getElementsByTagName('button')[0];
let text=document.getElementsByName('text')[0];
let text=document.getElementsByClassName('red')[0];
let list=document.getElementById('id1');
<script>点击 "运行实例" 按钮查看在线实例
2、实战: 在线聊天机器人
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>DOM实战:模拟智能在线***系统</title>
<style type="text/css">
div:nth-child(1) {
width: 450px;
height: 650px;
background-color: lightskyblue;
margin: 30px auto;
color: #333;
box-shadow: 2px 2px 2px #808080
}
h2 {
text-align: center;
margin-bottom: -10px;
}
div:nth-child(2) {
width: 400px;
height: 500px;
border: 4px double green;
background-color: #efefef;
margin: 20px auto 10px;
}
ul {
list-style: none;
line-height: 2em;
/*border: 1px solid red;*/
overflow: hidden;
padding: 15px;
}
table {
width: 90%;
height:80px;
margin: auto;
}
textarea{
/*width: 300px;*/
border: none;
resize: none;
background-color: lightyellow;
}
button {
width: 60px;
height: 40px;
background-color: seagreen;
color: white;
border: none;
/*text-align: left;*/
}
button:hover {
cursor: pointer;
background-color: orange;
}
</style>
</head>
<body>
<div>
<h2>在线***</h2>
<div contenteditable="true">
<ul>
<li></li>
</ul>
</div>
<table>
<tr>
<td align="right">
<textarea name="text" cols="50" rows="4"></textarea>
</td>
<td align="left"><button type="button">发送</button></td>
</tr>
</table>
</div>
<script>
let btn=document.getElementsByTagName('button')[0];
let text=document.getElementsByName('text')[0];
let list=document.getElementsByTagName('li')[0];
let sum=0;
btn.onclick=function(){
if(0===text.value.length){
alert("内容为空,请重新输入!");
return false;
}
let userInput=text.value;
let userLi=document.createElement('li');
let userPic='<img src="pic/user.jpg" width="30" style="border-radius:50%">';
userLi.innerHTML=userPic+userInput;
list.appendChild(userLi);
text.value='';
setTimeout(function(){
let replyMsg=[
'戒为良***!不泄为补!',
'她们只是诱饵,症状才是里面的钩子,症状是不会放过你的。',
'他们喜欢,我害怕,因为我深知危害之惨烈!',
'出症状了,就知道怕了!',
'你让自己爽,症状让你苦!',
'从天堂到地狱,你撸过人间。',
'戒到死,戒到地球爆炸!',
'不戒,你就完了,戒了,就是重生!',
'沉迷手淫,必招症状缠身,这是几千个案例揭示的真理!',
'把戒色当作人生最佳的历练,Just 戒 it!',
'你掏空自己的***精,医院掏空你的***!所谓人财两空!'
];
let randomNum=Math.floor(Math.random()*replyMsg.length);
let replyPic='<img src="pic/reply.jpg" width="30" style="border-radius:50%">';
let replyLi=document.createElement('li');
replyLi.innerHTML=replyPic+replyMsg[randomNum];
list.appendChild(replyLi);
},2000);
};
</script>
</body>
</html>点击 "运行实例" 按钮查看在线实例
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号