使用javascript,如何使用ON和OFF更改不透明度?

时间:2017-01-09 16:02:29

标签: javascript html css

我希望用户可以通过单击ON(不透明度100%)或OFF(不透明度0%)而不是1或0来更改不透明度。这可能吗? 代码:

<html><body>
<p id="demo"></p>

<script>

function validato()
{



 document.getElementById("demo").innerHTML = emailio;

}

function emalio(x,y){

function ValidateEmail(mail) 
{
 if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(myForm.emailAddr.value))
  {
    return (true)
  }
    alert("You have entered an invalid email address!")
    return (false)
}
 

}

</script>

</body></html>

1 个答案:

答案 0 :(得分:2)

如果您可以将options更改为具有值,则可以执行以下操作:

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>