我正在尝试获取用于chrome扩展的开关的状态,以了解其用途,下面是我的示例代码,用于访问切换开关onchange
事件并更改某些文本的颜色。
代码如下:
document.addEventListener('DOMContentLoaded', function() {
var togBtn = document.getElementById('togBtn');
togBtn.addEventListener('onchange', function() {
document.getElementById("demo").style.color = "red";
});
});
<label class="switch">
<input type="checkbox" id="togBtn" value="false" name="disableYXLogo">
<span class="slider round"></span>
</label>
如何获取拨动开关的状态并执行所需的任务?
答案 0 :(得分:0)
使用以下内容:
document.addEventListener('DOMContentLoaded', function() {
var togBtn = document.getElementById('togBtn');
togBtn.addEventListener('change', function() {
onChnage();
});
function onChnage() {
document.getElementById("demo").style.color = togBtn.checked ? "red" : "blue";
}
onChnage();
});
<label class="switch">
<input type="checkbox" id="togBtn" value="false" name="disableYXLogo">
<span class="slider round"></span>
</label>
<div id='demo'>I am the text of demo.</div>