我希望用户可以通过点击打开(不透明度 100%)或关闭(不透明度 0%)而不是 1 或 0 来更改不透明度。这可能吗? 代码:
<!DOCTYPE html>
<html>
<body>
<p id="p1">Change this phrase's opacity</p>
<select onchange="myFunction(this);" size="2">
<option>0 <!--I want this OFF not 0-->
<option selected="selected">1 <!--I want this ON not 1-->
</select>
<script>
function myFunction(x) {
var opacity = x.options[x.selectedIndex].text;
var el = document.getElementById("p1");
if (el.style.opacity !== undefined) {
el.style.opacity = opacity;
} else {
alert("Your browser doesn't support this!");
}
}
</script>
</body>
</html>
最佳答案
如果您可以将选项
更改为具有值,您可以执行以下操作:
function myFunction(x) {
var opacity = x.options[x.selectedIndex].value;
var el = document.getElementById("p1");
if (el.style.opacity !== undefined) {
el.style.opacity = opacity;
} else {
alert("Your browser doesn't support this!");
}
}
<p id="p1">Change this phrase's opacity</p>
<select onchange="myFunction(this);" size="2">
<option value="0">OFF</option>
<option value="1" selected="selected">ON</option>
</select>
关于javascript - 使用 javascript,如何通过 ON 和 OFF 更改不透明度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41551980/