JavaScript 系列一:第7課
認識巢狀結構
by applelily
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>線上下單</title>
<link rel="stylesheet" href="./js_else_if.css">
</head>
<body>
<div class="bk_img_container">
<div class="bk_img">
<img src="https://imgur.com/Y4gjfmp.jpg">
</div>
<div class="orderPage">
<div class="order_input_container">
<div class="list_items">
<span class="inline">
您好,尊姓大名?
<input class="input input_cust list_itms" id="customer_name" type="text" autofocus />
</span>
<span class="inline">
服裝的分類:
<select id="list_item_1">
<option value="men">男裝</option>
<option value="women">女裝</option>
</select>
</span>
<span class="inline">
服裝的類型:
<select id="list_itme_2">
<option value="coats">外套</option>
<option value="top">上衣</option>
<option value="bottom">下身</option>
</select>
</span>
<span class="inline">
訂購數量:
<input class="input list_itms" id="order_qty" type="text"/>
<p class="price_bookmark">
( 男裝:600元/件 ,女裝:500元/件 )</p>
</span>
</div>
<div class="btn_container">
<button class="btn btn_1" onclick="goOrder()">我要訂購</button>
<button class="btn btn_2" onclick="aboutMe()">認識工廠</button>
</div>
</div>
<div...
CSS
body{
color: #333;
background-color: #eedada;
font-family: OpenSansRegular, "微軟正黑體修正", "微軟正黑體", "Microsoft JhengHei", MHei, STHeitiTC-Light, sans-serif, Arial, Helvetica, "Helvetica Neue", Tahoma, Verdana;
}
.bk_img_container {
position: relative;
overflow-y: scroll; /* 只有垂直滾動條 */
overflow-x: hidden; /* 隱藏水平滾動條 */
width: 100%;
height: 98vh; /* 占满整个视口高度 */
}
.bk_img {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
object-fit: cover; /* 铺满且不变形 */
z-index: -1;
}
.orderPage{
position: absolute;
top: 20px;
left:20px;
background-color: rgba(238, 218, 218 , 50%) ;
padding-left: 20px;
font-size: 1.2rem;
color: #653705;
}
.order_input_container{
position: relative;
width: 100%;
height: 290px;
}
.list_items {
margin-top: 8px;
display: flex;
flex-direction: column;
align-items: flex-start;
}
.inline {
display: inline;
padding-top: 12px;
}
.input{
margin-top: 6px;
width: 150px;
height: 30px;
border: 1px solid #653705 ;
background-color: white;
}
.input_cust:focus {
outline: none !important;
border-color: #653705;
box-shadow: 0 0 10px #653705;
}
select {
font-size: 17px;
background: rgb(101, 55, 5, 70%);
color: #fafafa;
width: 100px;
height: 2em;
padding: 6px;
position: relative;
border-radius: 2px;
}
select:focus {
border: none;
font-size: 20px;
}
select::after {
font-size: 15px;
}
.price_bookmark {
font-size: smaller;
display: inline;
margin:0;
margin-right: 10px;
color: white;
background-color: #653705 ;
padding: 5px 10px;
}
.btn_container{
margin-top: 20px;
margin-left: 220px;
}
.btn {
margin-right: 10px;
font-size: 1.2rem;
width: 150px;
height: 50px;
background-color: #ffeae4;
color: #653705;
border:0px;
border-radius: 5px;
cursor: pointer;
}
.btn_2{
...
JavaScript
function goOrder(){
document.querySelector('.order_container').style.display = 'none';
const custName= document.getElementById('customer_name').value;
let order_qty = document.getElementById('order_qty').value;
let msg = '';
order_qty = parseInt(order_qty);
if ((custName == '') || (custName == null)) {
msg = "請輸入姓名 !!" ;
document.getElementById('customer_name').focus();
return alert(msg);
}
if (isNaN(order_qty)){
msg = "煩請輸入訂購數量" ;
document.getElementById('order_qty').focus();
return alert(msg);
}
if (order_qty < 100) {
msg = "抱歉,我們工廠的最低出貨量是 100 件。" ;
document.getElementById('order_qty').focus();
return alert(msg);
}
if (order_qty > 5000) {
msg = "抱歉,我們工廠的最高出貨量是 5,000 件。" ;
document.getElementById('order_qty').focus();
return alert(msg);
}
{
const myMsg = '謝謝您對我們的衣服有興趣!請致電 0987-654-321,會有專人提供您報價!';
const msg = custName + ', '+ myMsg;
alert(msg);
// show 出訂單訊息
document.querySelector('.order_container').style.display = 'block';
document.getElementById('order_list_customer_name').textContent=custName;
let menu_1 = document.getElementById('list_item_1');
let menu_1_index = menu_1.selectedIndex;
let menu_1_value = menu_1.options[menu_1_index].value;
let menu_1_text = menu_1.options[menu_1_index].text;
document.getElementById('order_list_item_1').textContent=menu_1_text;
let price = 0;
let promotion_msg = '「最近剛好是工廠週年慶,只要您今天來電下單,'
if (menu_1_value == 'men') {
price = 600;
alert( promotion_msg+'我們將贈送您帥氣領帶!」');
}
if (menu_1_value == 'women'){
price = 500;
alert( promotion_msg+'我們將贈送您美麗圍巾!」');
}
let menu_2 = document.getElementById('list_itme_2');
let menu_2_index = menu_2.selectedIndex;
let menu_2_value = menu_2.options[menu_1_index].value;
...