voteable = (age < 18) ? "好年轻":"够成熟";

如果变量 age 的值小于 18,变量 voteable 的值将是 "太年轻",否则变量 voteable 的值将是 "足够成熟"。

 

操作例子

<button onclick="myFunction()">试一试</button>

<p ></p>

<script>
function myFunction() {
var age, voteable;
age = document.getElementById("age").value;
voteable = (age < 18) ? "好年轻":"够成熟";
document.getElementById("demo").innerHTML = voteable;
}
</script>

https://www.w3school.com.cn/tiy/t.asp?f=js_comparison_17_ternary

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-07-16
  • 2021-10-31
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-09-18
  • 2021-09-15
  • 2022-12-23
  • 2022-12-23
  • 2020-07-18
  • 2022-12-23
相关资源
相似解决方案