javascript - 如何从 document.querySelectorAll() 正确循环遍历 NodeList?
问题描述
在学习不同 AJAX 请求的练习中,我想在#quote
每次单击按钮时应用淡入淡出转换。(渐变效果不是练习的一部分,只是我自己添加的)
我知道当有一个按钮(或只有第一个按钮)时如何执行此操作document.querySelector()
,但我了解到它document.querySelectorAll()
以数组的形式返回一个静态 NodeList,您将需要遍历数组以对每个按钮执行某些操作。
我已经尝试了几件事,但我无法弄清楚。
这就是我到目前为止所拥有的,据我所知,这 2 位代码需要在彼此内部,我的问题是如何.
// Quote animation
var buttons = document.querySelectorAll("button");
[].forEach.call(buttons, function(button){
// quote should have .fade applied every time one of the four buttons is clicked
});
$(button).addEventListener("click", function(){
$(quote).addClass("fade");
$(quote).bind('oanimationend animationend webkitAnimationEnd', function(){
$(this).removeClass("fade");
});
})
解决方案
这将选择每个按钮并触发您的淡入淡出效果
$("button").on('click', function(){
$(quote).addClass("fade");
$(quote).bind('oanimationend animationend webkitAnimationEnd', function(){
$(this).removeClass("fade");
});
});
推荐阅读
- python - 从值列表中添加或更新熊猫数据框
- tensorflow - “无法解压不可迭代的函数对象”:尝试通过 tensorflow 运行代码
- java - 我可以用 xdocreport 做什么来合并表格中的单元格?
- android - SplitInstallManager 的 SplitInstallStateUpdatedListener 无法正常工作(App Bundle)
- ios - UICollectionView 更改 Drop Placeholder 索引路径
- excel - 如何使用声明为 Variant 的变量实现 Vlookup?
- python-3.x - Jinja 检查每个循环索引值
- api-management - 在 Gravitee 中收集和使用自定义指标作为策略
- php - 有效阻止包含禁止网址的评论
- python - 如何在没有模块的情况下格式化 csv 文件(例如省略第一行和第一列)?