JavaScript 系列二:第7課

用 Selector API 選取元素

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_DOM.css">
</head>
<body>
    <div class="bg-container">
       
        <div class="input_text">
            <h1>
                To Do List
            </h1>
            <div class="input_container">
                <input type="text" id="to_do" class="to_do" placeholder="新增待辦事項...">
                <select id="list_level"  class="list_level" >
                    <option value="normal" selected>一般</option>
                    <option value="important" >重要</option>
                    <option value="urgent" >緊急</option>
                </select>
                <button  id ="add_list" class="add_list" onclick="addList()">+</button>
                <button class="export_btn" onclick="exportList()">匯出清單</button>
            </div>


        </div>
        <div class="to_do_list">
            <h3>待辦事項的清單</h3>
            <div class="to_do_list_container">
                <ul id="to_do_list_ul" class="to_do_list_ul">

                </ul>
            </div>
        </div> 
    </div>
    <script src="./js_DOM.js"></script>
</body>
</html>

CSS

* {
    margin: 0;
    padding: 0;
}
body{
    color: #82715f;
}
 .bg-container{
    background-image: url('https://imgur.com/0GIPhB0.jpg');
    width: 640px;
    min-height: 100px;
    border-radius: 8px;
    margin:auto;
    padding-top: 20px;
    padding-bottom: 30px;
    
} 

select{
    padding: 5px 10px;
    /* height: 2.2em; */
    font-size: 18px;
}
.input_text h1{
    margin-left: 40px;
}
.input_container{
    margin-left: 40px;
    width: 560px;
    display: flex;
    
}

.input_text input{
    min-width: 300px;
    /* height: 2em; */
    font-size: 18px;
    padding-left: 10px;
    
}

.add_list{
    border: none;
    color: #fff;
    background-color: #82715f;
    width: 50px; 
    font-size: 30px; 

    
}

.export_btn{
    border: none;  
    color: #fff;
    background-color: #82715f;
    width: 100px; 
    border-radius: 8px; 
    margin-left: 10px;
    padding: 5px 10px;
    font-size: 18px;
    cursor: pointer;

}

.to_do_list{
    background-color: #f2eee2;
    border-radius: 10px;
    width: 530px;
    min-height: 500px;
    margin: auto;
    margin-top: 20px;
    padding: 10px 10px;

}
.to_do_list h3{
    text-align: center;
    line-height: 2;
    letter-spacing: 0.3em;
    border-bottom: 1px dashed #C8C8C8 ;
}


.to_do_list_ul{
    padding-left: 20px;
}

.to_do_list_ul li{
    padding: 10px 10px;
    border-bottom: 1px dashed #C8C8C8 ;
}
.important_li{
    color:#ff8c00;
}

.urgent_li{
    color:#ff0000;
}

.del_btn{
    float: right;
    width: 25px;
    height: 25px;
    color: #fff;
    background-color:#82715f;
    font-size: 20px;

}
.del_btn:hover{
    background-color: #ff0000;
    cursor: pointer;
}

.on_off_btn {
    float: right;
    /* padding-right: 5px;
    border: none;
    color: #82715f;
    font-size: 30px; */
    float: right;
    width: 25px;
    height: 25px;
    /* color: #82715f; */
    border: 1px solid #82715f;
    font-size: 20px;

}

.done{
    with: 60px;
    padding-left: 5px;
    display:none;
}

JavaScript

let elem_toDo =  document.querySelector('#to_do')  ; 
let elem_toDo_class= elem_toDo.className ;
elem_toDo.addEventListener("keypress",
    function(event)
    {
        if(event.key === "Enter") {
            document.querySelector('#add_list').click();
        }
    }
)



let elem_select = document.querySelector("#list_level");
let elem_select_class= elem_select.className ;
// 監聽選擇框的變化
elem_select.addEventListener("change", 
    function() {
        // 取得當前選擇的選項值
        let selectedValue = elem_select.value;
        // 根據選擇的值來決定文字顏色
        if (selectedValue === "important") {
            elem_select.className = elem_select_class + 
            " important_li";
            elem_toDo.className = 
            elem_toDo_class +
            " important_li";
        } else if (selectedValue === "urgent") {
            elem_select.className = 
            elem_select_class + 
            " urgent_li";
            elem_toDo.className = 
            elem_select_class +
            " urgent_li";
        } 
        else {
            elem_select.className = elem_select_class;
            elem_toDo.className = elem_toDo_class
        } 
    }
);



function addList(){
    let toDoValue = document.querySelector('#to_do').value;
    let elem_value_select = 
    document.querySelector('#list_level').value;


    if (toDoValue!="") {

        let elem_li = document.createElement('li');

        if (elem_value_select == 'important'){
            elem_li.className = "important_li";
        }
        if (elem_value_select == 'urgent'){

            elem_li.className = "urgent_li";
        }


        let elem_span =  document.createElement('span');

        let elem_ul = document.querySelector('#to_do_list_ul');

        elem_ul.append(elem_li);
        elem_li.append(elem_span);
        elem_span.textContent= toDoValue;
        elem_span.value=toDoValue;

        let elem_span_done =  document.createElement('span');

        elem_span.append(elem_span_done );
       ...