javascript运算符
Example:
例:
Here, we are inputting age of the person and checking whether person is eligible for voting or not in JavaScript?
在这里,我们要输入人员的年龄并检查该人员是否有资格使用JavaScript投票?
JavaScript code (along with HTML):
JavaScript代码(以及HTML):
<!DOCTYPE html>
<HTML>
<HEAD>
<SCRIPT>
function Check(){
var age=parseInt(document.getElementById("txtage").value);
var status = (age>=18)?"Eligible":"Not Eligible";
document.getElementById("ans").innerHTML=status;
}
</SCRIPT>
</HEAD>
<BODY>
<h2>Ternary Operator</h2>
<hr />
<table>
<tr>
<td>
<label>Enter Age:</label>
</td>
<td>
<input type="text" name="txtage" id="txtage" />
</td>
</tr>
<tr>
<td>
</td>
<td>
<input type="button" value="Check Vote Eligiblity" onclick="Check()" />
</td>
</tr>
<tr>
<td>
<label>Status</label>
</td>
<td>
<span id="ans">
</span>
</td>
</tr>
</table>
</BODY>
</HTML>
Output
输出量
![JS | Ternary Operator in JS](https://i-blog.csdnimg.cn/blog_migrate/2a4cd530e3d9ec65142408813043e689.png)
翻译自: https://www.includehelp.com/code-snippets/ternary-operator-examples-in-javascript.aspx
javascript运算符