- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
通过 addEventListner()。我使用的是 vanilla,-6ren">
我在一个页面上有多个 input type="checkbox"
的 Switch 组件,我想在这些.active
上单独切换> 通过 addEventListner()
。我使用的是 vanilla,而不是 jQuery。
我让它在第一个元素上工作,但其他元素都不起作用。我需要他们都独立工作。
显然我没有正确定位“这个”。我使用函数声明、表达式、标准函数语法和箭头的组合对其进行了单独的尝试。通过箭头,我的目标是使用词法范围来影响正确的“this”范围。
这是一个 Codepen 尝试 3 次;打开控制台以查看第一个 Switch 元素上的 .active
类切换。
这是一个示例(也在笔中):
// Working on ONE checkbox:
let fireMySwitch = function(inputType, className) {
let mySwitchInput = document.querySelector(`input[type=${inputType}`);
mySwitchInput.addEventListener('change', function (event) {
!this.checked ? this.classList.remove(className) : this.classList.add(className);
}, false);
}
fireMySwitch('checkbox', 'active');
这是标记:
<div class="cell my-switch__block">
<p class="my-switch__copy">Switch</p>
<div class="switch large">
<input class="switch-input" id="yes-no_2" type="checkbox" name="mySwitch_2">
<label class="switch-paddle" for="yes-no_2">
<span class="show-for-sr">Switch</span>
<span class="switch-active" aria-hidden="true">Yes</span>
<span class="switch-inactive" aria-hidden="true">No</span>
</label>
</div>
</div>
任何人都可以建议我如何正确地独立定位每个元素来切换类?
最佳答案
您有多个输入,因此您需要迭代与选择器匹配的所有输入,而不仅仅是一个:
let fireMySwitch = function(inputType, className) {
for (const mySwitchInput of document.querySelectorAll(`input[type=${inputType}`)) {
mySwitchInput.addEventListener('change', function(event) {
!this.checked ? this.classList.remove(className) : this.classList.add(className);
});
}
}
fireMySwitch('checkbox', 'active');
.active + * {
background-color: green !important;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/foundation/6.5.0/css/foundation.min.css" rel="stylesheet"/>
<div class="grid-x grid-padding-x small-up-2 align-center-middle align-stretch my-switch__outer">
<div class="cell my-switch__block">
<p class="my-switch__copy">Switch</p>
<div class="switch large">
<input class="switch-input" id="yes-no_1" type="checkbox" name="mySwitch_1">
<label class="switch-paddle" for="yes-no_1">
<span class="show-for-sr">Switch</span>
<span class="switch-active" aria-hidden="true">Yes</span>
<span class="switch-inactive" aria-hidden="true">No</span>
</label>
</div>
</div>
<div class="cell my-switch__block">
<p class="my-switch__copy">Switch</p>
<div class="switch large">
<input class="switch-input" id="yes-no_2" type="checkbox" name="mySwitch_2">
<label class="switch-paddle" for="yes-no_2">
<span class="show-for-sr">Switch</span>
<span class="switch-active" aria-hidden="true">Yes</span>
<span class="switch-inactive" aria-hidden="true">No</span>
</label>
</div>
</div>
<div class="cell my-switch__block">
<p class="my-switch__copy">Switch</p>
<div class="switch large">
<input class="switch-input" id="yes-no_3" type="checkbox" name="mySwitch_3">
<label class="switch-paddle" for="yes-no_3">
<span class="show-for-sr">Switch</span>
<span class="switch-active" aria-hidden="true">Yes</span>
<span class="switch-inactive" aria-hidden="true">No</span>
</label>
</div>
</div>
<div class="cell my-switch__block">
<p class="my-switch__copy">Switch</p>
<div class="switch large">
<input class="switch-input" id="yes-no_4" type="checkbox" name="mySwitch_4">
<label class="switch-paddle" for="yes-no_4">
<span class="show-for-sr">Switch</span>
<span class="switch-active" aria-hidden="true">Yes</span>
<span class="switch-inactive" aria-hidden="true">No</span>
</label>
</div>
</div>
</div>
您还可以考虑使用classList.toggle
来代替:
let fireMySwitch = function(inputType, className) {
for (const mySwitchInput of document.querySelectorAll(`input[type=${inputType}`)) {
mySwitchInput.addEventListener('change', function(event) {
this.classList.toggle(className);
});
}
}
fireMySwitch('checkbox', 'active');
.active + * {
background-color: green !important;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/foundation/6.5.0/css/foundation.min.css" rel="stylesheet"/>
<div class="grid-x grid-padding-x small-up-2 align-center-middle align-stretch my-switch__outer">
<div class="cell my-switch__block">
<p class="my-switch__copy">Switch</p>
<div class="switch large">
<input class="switch-input" id="yes-no_1" type="checkbox" name="mySwitch_1">
<label class="switch-paddle" for="yes-no_1">
<span class="show-for-sr">Switch</span>
<span class="switch-active" aria-hidden="true">Yes</span>
<span class="switch-inactive" aria-hidden="true">No</span>
</label>
</div>
</div>
<div class="cell my-switch__block">
<p class="my-switch__copy">Switch</p>
<div class="switch large">
<input class="switch-input" id="yes-no_2" type="checkbox" name="mySwitch_2">
<label class="switch-paddle" for="yes-no_2">
<span class="show-for-sr">Switch</span>
<span class="switch-active" aria-hidden="true">Yes</span>
<span class="switch-inactive" aria-hidden="true">No</span>
</label>
</div>
</div>
<div class="cell my-switch__block">
<p class="my-switch__copy">Switch</p>
<div class="switch large">
<input class="switch-input" id="yes-no_3" type="checkbox" name="mySwitch_3">
<label class="switch-paddle" for="yes-no_3">
<span class="show-for-sr">Switch</span>
<span class="switch-active" aria-hidden="true">Yes</span>
<span class="switch-inactive" aria-hidden="true">No</span>
</label>
</div>
</div>
<div class="cell my-switch__block">
<p class="my-switch__copy">Switch</p>
<div class="switch large">
<input class="switch-input" id="yes-no_4" type="checkbox" name="mySwitch_4">
<label class="switch-paddle" for="yes-no_4">
<span class="show-for-sr">Switch</span>
<span class="switch-active" aria-hidden="true">Yes</span>
<span class="switch-inactive" aria-hidden="true">No</span>
</label>
</div>
</div>
</div>
关于javascript - 尝试使用普通 addEventListener 单独定位多个 Switch 组件,但只命中第一个 - 我没有正确使用 'this',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60469939/
我想在按下按钮时打开一个表单,然后在完成表单后创建另一个按钮以将数据存储在对象中。 我这样做了,但在第二个 addEventListener 中它给了我这个错误: cannot read proper
这是我实际代码的简化版本。 有三个复选框和按钮(每个编号为 0、1、2)和一个提交按钮。简而言之,如果您选中复选框编号 1 和 2,单击提交,按钮 1 和 2 将被着色。现在您可以单击任何彩色按钮,它
问题代码 var el = document.getElementById("searchcharm_'"+touch_id+"'"); el.addEventListener('click',
window.document.addEventListener = function(event) {...} window.addEventListener = function(event) {
这段代码块有什么区别: var xhr = new XMLHttpRequest(); xhr.upload.addEventListener("progress", uploadProgress,
在使用 PhoneGap 时,它有一些使用 document.addEventListener 的默认 JavaScript 代码,但我有自己的代码使用 window.addEventListener
当我运行这段代码时,为什么 .body 事件先触发? document.addEventListener('click', function() { console.log('The docume
我将带有循环的 addEventListener 添加到一些 anchor 元素 products 中,如下所示: for(var j=0;j
所以我的代码可以按我想要的方式工作,但是大多数(IE 除外)调试器都会向我抛出此错误: 未捕获类型错误:无法读取未定义的属性“addEventListener” 这是代码: //Get all ele
我真的不知道为什么这不起作用。在我的 HTML 中,如果我将脚本放在 head 部分,我会在控制台中收到以下错误: Uncaught TypeError: Cannot read property '
在 React 中,当你有一个带有 onClick 属性的元素时,很容易使用 Enzyme 的 .simulate("click") 方法来点击它。然而,在我的代码库中有一个示例,其中一个元素被 Re
问候,由于某种原因,当我单击按钮时,我的 -addEventListener 没有执行。任何人都可以帮忙这是我的代码: function elNegro() { alert("Thank Yo
我正在将我的一些代码从 onClick 更改为 addEventListener,因为我已经阅读了各种好处,并且出于关注点分离的目的。然而,我遇到的问题之一是,虽然使用 onClick,我能够调用一个
我正在尝试向我在循环中生成的某些元素添加事件监听器。我必须使用 div.lastChild - 虽然在这个例子中它非常愚蠢。但这只是演示: var func = function() {
我在将事件监听器添加到 JS 时遇到问题...如果我将按钮与 onclick 属性连接,它会起作用,但如果我在 HTML 中删除它并尝试添加 eventListener('click', myFunc
我不知道为什么,但是当我点击“按钮”时什么也没有发生... 控制台中没有消息,没有错误。如何解决? JS var bird = (function(){ let button = doc
我正在绘制一个方框网格,希望能够单击每个方框并使用 SocketIO 将带有其 ID 的通知发送到服务器 let boxes = document.querySelecto
我想是新手问题。 以下代码是我在文档就绪时调用的函数的一部分。它旨在在鼠标移动时永久返回当前鼠标位置的值。 正在发生的奇怪事情:在文档就绪时移动鼠标不会将任何内容记录到控制台。我知道 mouse_mo
我是 Flash 的新手,我似乎无法完成这个简单的操作。 (我正在使用 ActionScript 3.0) 我在我的编辑器中创建了一个输入文本框。实例名称是“测试”。在我的 Action 编辑器中,我
我有一个像 那些标签包含一些内容,我想打开/关闭它们,但仅使用纯 javascript。我已经尝试过使用 document.querySelectorAll
我是一名优秀的程序员,十分优秀!