【发布时间】:2020-10-10 04:43:02
【问题描述】:
您好,我在选择下拉菜单时遇到问题。我正在尝试存储该值,但它不起作用。 我已经看过其他问题了,但没有运气。
这是一个小提琴:
function selectCss(element) {
const a = element.options[element.selectedIndex].value;
document.getElementById("change").onchange = function() {
localStorage['colorPick'] = element.options[element.selectedIndex].value;
}
window.onload = function() {
if (localStorage['colorPick'])
document.getElementById("change").value = localStorage['colorPick'];
}
if (a == "Theme 1") {
document.body.style.background = "pink";
}
if (a == "Theme 2") {
document.body.style.background = "blue";
}
if (a == "Theme 3") {
document.body.style.background = "yellow";
}
}
body {
background: pink;
}
<select id="change" name="colorPick" onchange="selectCss(this)">
<option selected value="Theme 1">Theme 1</option>
<option value="Theme 2">Theme 2</option>
<option value="Theme 3">Theme 3</option>
</select>
【问题讨论】:
标签: javascript jquery css select dropdown