JavaScript 使用 if 语句中的的 OR 条件
JavaScript 包含条件语句,如 if-else
、switch case
等。这些语句是有条件的,用于检查给定的条件是否为真;为此,我们使用 OR ||
和 &&
运算符。
运算符 || (OR)
检查所有条件并确定任何条件是否为真。你困惑吗?不用担心,我们将通过示例进一步讨论以更好地理解|| (或)运算符
。
使用 IF
语句的 JavaScript OR
条件示例
下面的 HTML
源代码显示了一个表单输入,我们将使用它从用户那里获取输入字符串值,就像任何字母表一样,还有一个按钮提交,我们在点击时调用了 JavaScript 函数 checkVowel()
。
checkVowel()
函数包含一个带有 || 的
if 语句 条件(OR) operator
来检查用户输入的字母是否为元音。
<!DOCTYPE html>
<html>
<head>
<title>
HTML | Window Print() method example
</title>
<script type="text/javascript">
</script>
</head>
<body>
<h2>Hi Users Check your choosen alphabet is a vowel or not.</h2>
<form id="form" onsubmit="return false;">
<input type="text" id="userInput" maxlength="1" placeholder="Enter any alphabet" />
<input type="submit" onclick="checkVowel();" />
</form>
<script>
function checkVowel() {
var variable = document.getElementById("userInput").value;
if(variable =="a" || variable=="A"){
alert(variable+" is vowel")
}
else if(variable =="e" || variable=="E"){
alert(variable+" is vowel")
}
else if(variable =="i" || variable=="I"){
alert(variable+" is vowel")
}
else if(variable =="o" || variable=="O"){
alert(variable+" is vowel")
}
else if(variable =="u" || variable=="U"){
alert(variable+" is vowel")
}
else{
alert(variable+" is not vowel")
}
}
</script>sss
</body>s
<html>
在这个 HTML
页面源代码中,我们创建了一个表单输入类型的文本来从用户那里获取字母表。
你可以看到触发 checkVowel()
方法的提交按钮。
在 checkVowel()
方法的主体中,我们使用 ||
实现了一些 if-else
条件(OR) 运算符。如果任何 if
语句满足要求的条件,这将检查用户给定的带有 UPPER CASE
和 LOWER CASE
元音的字母表,该方法将显示一个弹出警告消息,即给定字母表是元音
。
如果给定的值与任何给定的条件不匹配,程序将执行 else condition
,显示警告消息给定的字母表不是元音。
用几个步骤检查给定的 HTML
代码
按照这四个简单的步骤,可以清楚地了解||
(OR) 运算符。
-
创建一个文本文档。你可以使用记事本或任何其他文本编辑工具。
-
将给定的代码粘贴到创建的文本文件中。
-
使用
.html
扩展名保存该文本文件,并使用任何默认浏览器打开它。 -
你可以看到输入表单字段输入任何字母并按提交按钮;它将显示一个警告框,显示消息字母表是否为元音。
获得相同结果的替代方法
你还可以使用 ||
使用 switch
语句实现相同的功能(OR) 运算符,如下所示。
switch (variable) {
case 'a' || 'A':
alert(variable + ' is vowel')
break;
只需将 switch
(变量)与包含 ||
的 case
一起使用运算符,而不是使用 if
语句。
相关文章
Do you understand JavaScript closures?
发布时间:2025/02/21 浏览次数:108 分类:JavaScript
-
The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.
Do you know about the hidden traps in variables in JavaScript?
发布时间:2025/02/21 浏览次数:178 分类:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
发布时间:2025/02/21 浏览次数:150 分类:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
用 jQuery 检查复选框是否被选中
发布时间:2024/03/24 浏览次数:102 分类:JavaScript
-
在本教程中学习 jQuery 检查复选框是否被选中的所有很酷的方法。我们展示了使用直接 DOM 操作、提取 JavaScript 属性的 jQuery 方法以及使用 jQuery 选择器的不同方法。你还将找到许多有用的
jQuery 中的 Window.onload 与 $(document).ready
发布时间:2024/03/24 浏览次数:180 分类:JavaScript
-
本教程演示了如何在 jQuery 中使用 Window.onload 和 $(document).ready 事件。