HTML+CSS+JS實現(xiàn)的簡單應(yīng)用小案例分享
更新時間:2022年02月23日 16:54:51 作者:Emperor10
這篇文章主要為大家分享四個用HTML+CSS+JS實現(xiàn)的簡單應(yīng)用小案例,有:猜數(shù)字、表白墻、切換日夜間模式和待辦事項,需要的可以參考一下
1.猜數(shù)字
<!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>猜數(shù)字游戲</title>
</head>
<body>
<input type="button" id="reset" value="重新開始一局游戲">
<div>
<span>請輸入要猜的數(shù)字:</span>
<input type="text" id="num">
<input type="button" value="猜" id="guess">
</div>
<div>
<span>已經(jīng)猜的次數(shù): </span>
<span id="count"> 0 </span>
</div>
<div>
<span>猜的結(jié)果: </span>
<span id="result"></span>
</div>
<script>
// 先獲取要用到的 JS 的 DOM 對象
let resetButton = document.querySelector("#reset") /* 參數(shù)時選擇器,所以要通過特殊符號指定是哪種選擇器*/
let numInput = document.querySelector('#num');
let guessButton = document.querySelector('#guess');
let countSpan = document.querySelector('#count');
let resultSpan = document.querySelector('#result');
//生成一個1~100之間的隨機整數(shù)
let toGuess = Math.floor(Math.random()*100) + 1;
let count = 0;
console.log("toGuess: " + toGuess);
guessButton.onclick = function(){
// 用戶點擊 [猜] 這個按鈕, 首先先更新點擊次數(shù)的顯示.
count++;
countSpan.innerHTML = count;
// 讀取到輸入框的內(nèi)容, 進行判定
/* parseInt() 函數(shù)解析字符串并返回整數(shù) */
let num = parseInt(numInput.value);
console.log("num: " + num);
if(num < toGuess){
resultSpan.innerHTML = '猜低了';
resultSpan.style.color = 'red';
}else if(num > toGuess){
resultSpan.innerHTML = '猜高了';
resultSpan.style.color = 'green';
}else{
resultSpan.innerHTML = '猜對了';
resultSpan.style.color = 'orange';
}
}
resetButton.onclick = function(){
// 把 toGuess 和 count 清空, 同時重新生成一個隨機的整數(shù)
toGuess = Math.floor(Math.random() * 100) + 1;
count = 0;
resultSpan.innerHTML = '';
countSpan.innerHTML = '';
numInput.value = '';
}
</script>
</body>
</html>

2.表白墻
<!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>messageWall</title>
</head>
<body>
<style>
*{
margin:0;
padding:0;
}
.container {
width: 100%;
height:100%;
}
h1{
text-align:center;
padding:20px 0;
}
p{
font-size: 15px;
color:grey;
padding:10px 0;
text-align: center;
}
.row{
display:flex;
height:50px;
justify-content: center;
align-items: center;
}
.row span{
width:100px;
}
.row .edit{
width: 200px;
height: 36px;
}
.row .submit{
width:300px;
height:30px;
background-color: orange;
color:#fff;
border: none; /* 去掉按鈕邊框*/
}
.row .submit:active{
background-color: grey;
}
</style>
<div class="container">
<h1>表白墻</h1>
<p>輸入后點擊提交, 將會把消息顯示在墻上</p>
<div class="row">
<span>誰:</span>
<input type="text" class="edit">
</div>
<div class="row">
<span>對誰:</span>
<input type="text" class="edit">
</div>
<div class="row">
<span>說什么:</span>
<input type="text" class="edit">
</div>
<div class="row">
<input type="button" value="提交" class="submit">
</div>
</div>
<script>
let submitButton = document.querySelector('.submit');
submitButton.onclick = function() {
// 1. 獲取到輸入框里面的內(nèi)容
let edits = document.querySelectorAll('.edit');
let from = edits[0].value;
let to = edits[1].value;
let message = edits[2].value;
console.log(from + ", " + to + ", " + message);
if (from == '' || to == '' || message == '') {
return;
}
//2. 創(chuàng)建一個新的元素節(jié)點,即獲取到的輸入框信息
//將其添加到DOM樹中
let row = document.createElement('div');/* 創(chuàng)建出新的idv節(jié)點*/
row.className = 'row';/* 設(shè)置屬性名 */
row.innerHTML = from + '對' + to + '說: ' + message;
let container = document.querySelector('.container');
container.appendChild(row);
//3. 把上次輸入的內(nèi)容清空
for(let i = 0; i < edits.length; i++){
edits[i].value = '';
}
}
</script>
</body>
</html>

3.切換日夜間模式
<!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>切換日夜間模式</title>
</head>
<body>
<style>
/* 清除瀏覽器默認格式 */
*{
margin:0;
padding: 0;
}
html,body {
width: 100%;
height: 100%;
}
.light{
background-color: #fff;
color: #000;
font-size: 40px;;
}
.dark{
background-color: #666;
color: #eee;
font-size: 40px;;
}
</style>
<div class="light">
代碼案例:切換日夜間模式;
</div>
<script>
/*獲取元素*/
let div = document.querySelector('div');
div.onclick = function(){
console.log(div.className); /* 獲取屬性值:content*/
if (div.className.indexOf('light') != -1) {
div.className = 'dark';
}else{
div.className = 'light';
}
}
</script>
</body>
</html>
4.待辦事項
<!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>todoList</title>
</head>
<body>
<style>
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
.nav {
width: 600px;
margin: 0 auto;
display: flex;
justify-content: center;
align-items: center;
}
.nav input {
width: 450px;
height: 50px;
font-size: 25px;
padding-left: 10px;
}
.nav button {
width: 150px;
height: 50px;
border: none;
color: white;
background-color: orange;
font-size: 18px;
}
.nav button:active {
background-color: grey;
}
.container {
width: 600px;
margin: 0 auto;
display: flex;
justify-content: center;
/* padding-top: 10px; */
margin-top: 10px;
/* background-color: green; */
}
.container .left,
.container .right {
width: 50%;
}
.container .left h3,
.container .right h3 {
text-align: center;
height: 50px;
line-height: 50px;
background-color: black;
color: white;
}
.container .row {
height: 50px;
display: flex;
align-items: center;
justify-content: flex-start;
}
.container .row span {
width: 240px;
}
.container .row button {
width: 40px;
height: 30px;
}
.container .row input {
margin-right: 5px;
}
</style>
<!-- 表示上方的 div, 里面放輸入框和按鈕 -->
<div class="nav">
<input type="text">
<button>新建任務(wù)</button>
</div>
<!-- 這個是下方的 div, 里面分成左右兩欄 -->
<div class="container">
<div class="left">
<h3>未完成</h3>
<!-- <div class="row">
<input type="checkbox">
<span>吃飯</span>
<button>刪除</button>
</div> -->
</div>
<div class="right">
<h3>已完成</h3>
</div>
</div>
<script>
let addTaskBtn = document.querySelector(".nav button");
addTaskBtn.onclick = function() {
// 1. 獲取到輸入框的內(nèi)容
let input = document.querySelector(".nav input");
let taskContent = input.value;
// 2. 創(chuàng)建一個 div.row, 里面設(shè)置上需要的 復(fù)選框, 文本, 刪除按鈕
let row = document.createElement('div');
row.className = 'row';
let checkBox = document.createElement('input');
checkBox.type = 'checkbox';
let span = document.createElement('span');
span.innerHTML = taskContent;
let deleteBtn = document.createElement('button');
deleteBtn.innerHTML = '刪除';
row.appendChild(checkBox);
row.appendChild(span);
row.appendChild(deleteBtn);
// 3. 把 div.row 添加到 .left 中~
let left = document.querySelector('.left');
left.appendChild(row);
// 4. 給 checkBox 增加一個點擊處理函數(shù). 點擊之后就能夠移動任務(wù)
checkBox.onclick = function() {
// 當(dāng)用戶點擊的時候, 就獲取到當(dāng)前的這個 row 這個元素
// 把這 row 給添加到另外一側(cè).
// 也可以根據(jù) checkBox 的選中狀態(tài)決定是在 left 還是 right
let target = null;
if (checkBox.checked) {
// 已經(jīng)是選中的狀態(tài)
// 就把這個元素放到右邊
target = document.querySelector('.right');
} else {
// 是未選中的狀態(tài)
// 就把這個元素放到左邊
target = document.querySelector('.left');
}
target.appendChild(row);
}
// 5. 實現(xiàn)刪除效果, 給刪除按鈕新增一個刪除操作
deleteBtn.onclick = function() {
// 要想刪除 row, 就需要知道 row 的父元素
let parent = row.parentNode;
parent.removeChild(row);
}
}
</script>
</body>
</html>

到此這篇關(guān)于HTML+CSS+JS實現(xiàn)的簡單應(yīng)用小案例分享的文章就介紹到這了,更多相關(guān)HTML CSS JS應(yīng)用案例內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
bootstrap switch開關(guān)組件使用方法詳解
這篇文章主要為大家詳細介紹了bootstrap switch開關(guān)組件的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08
httpclient模擬登陸具體實現(xiàn)(使用js設(shè)置cookie)
最簡單的方法就是通過得到的cookie定制一個httpclient,感興趣的朋友可以了解下本文2013-12-12

