Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
264 views
in Technique[技术] by (71.8m points)

javascript - How can I make appear a remove button on every LI in a Todo List?

This is my first post here in Stack Overflow. Apologies if I'm not doing it in the right way.

OK so I'm doing this To Do List as a second project but I can't find a solution yet.

I tried creating a node list, then going through the whole list, creating a new element and adding it to every item. Problem is, only the icon I added to the first LI in the HTML file is showing.

Am I doing it in the wrong way?

Any help is really appreciated!

This is how it looks like so far

HTML:

<title>To Do List</title>
</head>
<body>
    <div id="box">
        <div id="header">
            <h1 id="title">To Do List</h1>
            <span onclick="newElement()" id="addButton">+</span>
            <input type="text" id="newTask" class="hideMe" class="showMe" placeholder="New task...">
        </div>
        <div id="tasks">
            <ul id="items">
                <li class="checked">Go to the gym</li>
                <button class="remove" class="hideMe" class="showMe"><i class="fa fa-trash"></i></button>
                <li>Keep studying web development</li>
                <li>1</li>
            </ul>
        </div>
    </div>
    <script src="js/toDoList.js"></script>
</body>

CSS:

ul li {
    font-weight: 300;
    color: white;
    cursor: pointer;
    transition: 0.2s;
    list-style-type: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    display: inline-block;
    vertical-align: top;
    width: 55%;
    font-size: 35px;
    margin-left: 20px;
    margin-bottom: 25px;
}

ul li:hover {
    color: gray;
}

ul li.checked {
    text-decoration: line-through;
}

.remove {
    float: right;
    margin-right: 0.5em;
    font-size: 45px;
    color:rgb(255, 66, 66);
    background-color: rgba(255,255,255,0);
    border: none;
    transition: all 0.2s;
    outline: none;
    cursor: pointer;
}

.remove:hover {
    color:rgba(255, 66, 66, 0.589);
}

#addButton {
    font-size: 50px;
    text-align: center;
    cursor: pointer;
    font-weight: 900;
    transition: all 0.3s;
    color: rgb(255, 115, 0);
    background-color: rgb(225, 236, 255);
    border-radius: 50%;
    margin-top: 30px;
    margin-right: 35px;
    padding: 2px 18px 5px 18px;
    user-select: none;
    float: right;
}

#addButton:hover {
    background-color: rgba(225, 236, 255, 0.705);
    color: rgba(255, 115, 0, 0.678);
}

#box {
    min-width: 10em;
    max-width: 75em;
    height: auto;
    margin: auto;
    border-radius: 25px;
    background-color: rgba(70, 96, 180, 0.877);
    flex-grow: .67;
    object-position: center;
    padding-bottom: 1em;
}

#items {
    margin-top: 2em;
    margin-left: 1em;
}

#newTask {
    color: rgb(255, 115, 0);
    font-size: 30px;
    width: 100%;
    height: 3em;
    background-color: rgb(225, 236, 255);
    outline: none;
    border: none;
    border-radius: 0;
    display: inline-block;
}

#title {
    color: rgb(255, 115, 0);
    font-weight: bold;
    font-size: 50px;
    margin-left: 35px;
    display: inline-block;
}

JS:

const addButton = document.getElementById("addButton");
const newTaskInput = document.getElementById("newTask")
const removeButton = document.getElementsByClassName("remove");
var myNodeList = document.getElementsByTagName("LI");

function main() {
    for (let i = 0; i < myNodeList.length; i++) {
        let span = document.createElement("SPAN")
        span.className = "remove";
        span.appendChild(removeButton);
        myNodeList[i].appendChild(span);
    }
}

main();
question from:https://stackoverflow.com/questions/65835413/how-can-i-make-appear-a-remove-button-on-every-li-in-a-todo-list

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I do not know what is your end goal but for your next step try using this in your js

    document.querySelectorAll('#items li').forEach(li => {
    const button = document.createElement('button');
    button.className = 'remove';
    button.innerHTML = 'Remove';
    button.type = 'button';

    li.appendChild(button);
    })

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...