- HTML5 Web Application Development By Example Beginner's Guide
- J.M. Gustafson
- 179字
- 2021-08-13 16:50:20
Time for action – removing a task from the list
Now that we can add tasks to the list, let's add the ability to remove tasks. To do this we'll need a delete button for each task in the list. We'll add the code to create the button in the addTaskElement()
method. You can find the code for this section in Chapter 1/example1.2
.
function addTaskElement(taskName) { var $task = $("<li></li>"); var $delete = $("<button class='delete'>X</button>"); $task.append($delete) .append("<span class='task-name'>" + taskName + "</span>"); $delete.click(function() { $task.remove(); }); }
The first thing this method does is create a new <button>
element with a class of delete
. Then it creates the list item element as we did before, except that first it appends the delete button and then appends the task name. Note that we are now wrapping the task name in a <span class=
'task-name
'>
element to help us keep track of it. Last we add a click event handler to the delete button. To delete the task from the list element we simply call the remove()
method to remove it from the DOM. Voila, it's gone!
- JavaScript百煉成仙
- Boost程序庫(kù)完全開發(fā)指南:深入C++”準(zhǔn)”標(biāo)準(zhǔn)庫(kù)(第5版)
- 數(shù)據(jù)結(jié)構(gòu)和算法基礎(chǔ)(Java語(yǔ)言實(shí)現(xiàn))
- Python自動(dòng)化運(yùn)維快速入門(第2版)
- PostgreSQL技術(shù)內(nèi)幕:事務(wù)處理深度探索
- 機(jī)器人Python青少年編程開發(fā)實(shí)例
- 薛定宇教授大講堂(卷Ⅳ):MATLAB最優(yōu)化計(jì)算
- Mastering LibGDX Game Development
- Learning Network Forensics
- App Inventor創(chuàng)意趣味編程進(jìn)階
- Java語(yǔ)言程序設(shè)計(jì)教程
- Learning iOS Security
- Learning Nessus for Penetration Testing
- 大學(xué)計(jì)算機(jī)應(yīng)用基礎(chǔ)(Windows 7+Office 2010)(IC3)
- JavaScript Unit Testing