简体   繁体   English

如何删除子元素

[英]how to remove child elements

I am making a todo app with javascript, bootstrap, jquery. 我正在使用javascript,bootstrap,jquery创建一个todo应用程序。 I want to make the color of the checkbox darker when you click the task. 单击任务时,我想使复选框的颜色更暗。 What I'm thinking is to remove the child element i, but not the task (for example Python), and add a new child element i. 我在想的是删除子元素i,但不删除任务(例如Python),并添加一个新的子元素i。 I tried .empty() method but the thing is the task also gets removed. 我尝试过.empty()方法,但事情是任务也被删除了。 Is there any way that I can remove the element i but leave the task? 有什么方法可以删除元素但是离开任务吗?

 var lst = document.getElementsByTagName("li"); var icon = '<i class="far fa-check-square"></i>'; for (var i = 0; i < 3; i++) { $(lst[i]).append(icon); }; $(lst).click(function() { $(this).empty(); }); 
 body { padding: 0; margin: 0; } #container { width: 500px;} #container, #header, #tasks { border: 1px solid black; margin: 0 auto; } #header { text-align: center; padding: 0 0 20px 0;} input { padding: 10px; border: 0; border-bottom: 1px solid rgb(177, 177, 177); } span { padding: 7.5px; border: 1px solid rgb(177, 177, 177); background-color: rgb(219, 219, 219); } li { list-style-type: none; padding: 20px 15px; border: 1px solid black;} ul { padding: 0; margin: 0; } .fa-check-square { float: right; font-size: 25px; } 
  <script defer src="https://use.fontawesome.com/releases/v5.0.6/js/all.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="container"> <div id="header"> <h1>TODO</h1> <input type="text" id="newTask" placeholder="type your task here"> <span id="addTask">ADD</span> </div> <div id="tasks"> <ul> <li>Python</li> <li>Java</li> <li>React</li> </ul> </div> </div> 

Your have to use .remove(); 你必须使用.remove(); instead of .empty() 而不是.empty()

If you want to remove the whole row, you can $(this).remove(); 如果你想删除整行,你可以$(this).remove();

If you want to remove just the check icon, you can $(this).find('svg').remove(); 如果你只想删除支票图标,你可以$(this).find('svg').remove();


You can also use .hide() if you are planning to show it again. 如果您打算再次显示,也可以使用.hide() ex: $(this).find('svg').hide(); 例如: $(this).find('svg').hide();


 var lst = document.getElementsByTagName("li"); var icon = '<i class="far fa-check-square"></i>'; for (var i = 0; i < 3; i++) { $(lst[i]).append(icon); }; $(lst).click(function() { $(this).find('svg').remove(); }); 
 body { padding: 0; margin: 0; } #container { width: 500px;} #container, #header, #tasks { border: 1px solid black; margin: 0 auto; } #header { text-align: center; padding: 0 0 20px 0;} input { padding: 10px; border: 0; border-bottom: 1px solid rgb(177, 177, 177); } span { padding: 7.5px; border: 1px solid rgb(177, 177, 177); background-color: rgb(219, 219, 219); } li { list-style-type: none; padding: 20px 15px; border: 1px solid black;} ul { padding: 0; margin: 0; } .fa-check-square { float: right; font-size: 25px; } 
  <script defer src="https://use.fontawesome.com/releases/v5.0.6/js/all.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="container"> <div id="header"> <h1>TODO</h1> <input type="text" id="newTask" placeholder="type your task here"> <span id="addTask">ADD</span> </div> <div id="tasks"> <ul> <li>Python</li> <li>Java</li> <li>React</li> </ul> </div> </div> 

Doc: https://api.jquery.com/remove/ Doc: https//api.jquery.com/remove/

Find the i element then remove it. 找到i元素然后删除它。

$(lst).click(function() {
  $(this).find('i').remove();  
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM