gpt4 book ai didi

javascript - 在 Jsfiddle 中工作 - 不在站点/浏览器上工作

转载 作者:太空宇宙 更新时间:2023-11-04 15:51:13 25 4
gpt4 key购买 nike

我有一段可以在 Jsfiddle 中运行的 JavaScript 代码。当用户在下拉列表中选择"is"时,第二个自动选择“否”,反之亦然。我已将其格式化为 HTML 页面,但由于某种原因它不起作用。

我使用chrome作为浏览器。我已经添加了 jsfiddle 链接以及带有 JS 的 HTML 代码。

Jsfiddle链接:https://jsfiddle.net/rk57hkak/10/

HTML 代码:

<!DOCTYPE html>
<html>
<body>
<script src="committee.js"></script>
<div class="cmicrophone" id="cmicrophone">Currently:
<select id="cmicrophone" class="cmicrophone select1" name="cmicrophone" onchange="">
<option value="">Select Option</option>
<option value="'S microphone is currently on. Please remind them to turn it ">OFF</option>
<option value="'S microphone is currently off. Please remind them to turn it ">ON</option>
</select>
</div>
<div class="microphone" id="microphone">Microphone:
<select id="microphone" class="microphone select2" name = "microphone">
<option value=" " selected="selected">Select Option</option>
<option value="ON. Thank you.">ON</option>
<option value="OFF. Thank you.">OFF</option>
</select>
</div>
</body>
</html>

JavaScript 代码:

const cmicrophone = document.querySelector('.select1');
const microphone = document.querySelector('.select2');

function inputHandler(thisSelect, otherSelect) {
// console.log(thisSelect.selectedIndex)
if (thisSelect.selectedIndex == 1) {
otherSelect.selectedIndex = 1;
} else if (thisSelect.selectedIndex == 2) {
otherSelect.selectedIndex = 2;
} else {
thisSelect.selectedIndex = 0;
otherSelect.selectedIndex = 0;
}
}

cmicrophone.addEventListener('input', event => {
inputHandler(cmicrophone, microphone);
});

microphone.addEventListener('input', event => {
inputHandler(microphone, cmicrophone);
});

最佳答案

这是因为脚本是在元素出现在 DOM 中之前执行的。在这种情况下,行 const cmicrophone = document.querySelector('.select1'); & constmic = document.querySelector('.select2'); 由于元素不可用而失败

<body>
//html code
<script src="committee.js"></script> // Include script here
</body>

关于javascript - 在 Jsfiddle 中工作 - 不在站点/浏览器上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43096423/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com