javascript - 将列表的子元素推送到数组
问题描述
<div class="button">
<ul id='modes' class='flex-row-wrap'>
<li class="list-btn"><a href="#">Btn1</a>
<i class="favorite">
<svg aria-hidden="true" focusable="false" data-prefix="fas" data-icon="heart" class="svg-inline--fa fa-heart fa-w-16" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
<path fill="currentColor" d="M462.3 62.6C407.5 15.9 326 24.3 275.7 76.2L256 96.5l-19.7-20.3C186.1 24.3 104.5 15.9 49.7 62.6c-62.8 53.6-66.1 149.8-9.9 207.9l193.5 199.8c12.5 12.9 32.8 12.9 45.3 0l193.5-199.8c56.3-58.1 53-154.3-9.8-207.9z"></path>
</svg>
</i>
</li>
<li class="list-btn"><a href="#">Button Text here is very long 2</a>
<i class="favorite">
<svg aria-hidden="true" focusable="false" data-prefix="fas" data-icon="heart" class="svg-inline--fa fa-heart fa-w-16" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
<path fill="currentColor" d="M462.3 62.6C407.5 15.9 326 24.3 275.7 76.2L256 96.5l-19.7-20.3C186.1 24.3 104.5 15.9 49.7 62.6c-62.8 53.6-66.1 149.8-9.9 207.9l193.5 199.8c12.5 12.9 32.8 12.9 45.3 0l193.5-199.8c56.3-58.1 53-154.3-9.8-207.9z"></path>
</svg>
</i>
</li>
<li class="list-btn"><a href="#">Button 3</a>
<i class="favorite">
<svg aria-hidden="true" focusable="false" data-prefix="fas" data-icon="heart" class="svg-inline--fa fa-heart fa-w-16" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
<path fill="currentColor" d="M462.3 62.6C407.5 15.9 326 24.3 275.7 76.2L256 96.5l-19.7-20.3C186.1 24.3 104.5 15.9 49.7 62.6c-62.8 53.6-66.1 149.8-9.9 207.9l193.5 199.8c12.5 12.9 32.8 12.9 45.3 0l193.5-199.8c56.3-58.1 53-154.3-9.8-207.9z"></path>
</svg>
</i>
</li>
//JavaScript函数
function store() {
var favItem = document.getElementsByClassName('favorite');
var favorites =[];
var fav = favItem.children;
favorites.push(fav);
console.log(favItem);
console.log(favorites);
}
document.querySelectorAll(".favorite").forEach(item => {
item.addEventListener("click", store);
});
我试图在单击图标时将每个列表项存储在一个数组中,并再次单击该图标从数组中删除。但我不知道如何选择列表图标中的子元素。
解决方案
推荐阅读
- javascript - 如何在expo react native中将图像上传到firebase
- c++ - 是否有任何 C++ 函数可以对哈希表进行排序?
- android - 启用 PRODUCT_FULL_TREBLE_OVERRIDE 标志会影响 SELINUX 策略吗?
- python - Xpath没有给出结果scrapy python
- php - soapclient 请求 https 服务器错误,“您正在对启用 SSL 的服务器端口使用纯 HTTP”
- javascript - 如何在 Yarn 工作区的一个 package.json 文件中安装包?
- java - Spring批处理JobExecutionListener不起作用
- azure-logic-apps - ISE 中的逻辑应用程序 - 事件触发器(存储)不会触发,而 ISE 外部的逻辑应用程序会针对同一事件触发
- c++ - 如何创建从另一个线程到 Qt5 GUI 线程的 qDebug 信号
- reactjs - 样式 react-slick.neostack