1.if...else
let f = false;
let a;
if(f ===true){
a = 10;
}else{
a = 5;
}
结果展示:
不必直接明确指定 'a === true',例如:
let a = false;
let b;
if (a) {
b = 10;
} else {
b = 5;
}
结果展示:
2.else if
html文件中:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<label for="weather">选择今天的天气:</label>
<select id="weather">
<option value="">做出选择</option>
<option value="sunny">晴天</option>
<option value="rainy">雨天</option>
<option value="snowing">下雪天</option>
</select>
<p></p>
<script src="scripts/main1.js" defer></script>
</body>
</html>
js文件中:
const select = document.querySelector("select");
const para =document.querySelector("p");
select.addEventListener("change",setWeather);
function setWeather(){
const choice = select.value;
if(choice ==="sunny"){
para.textContent = "晴天晴天晴天"
}else if(choice === "rainy"){
para.textContent = "下雨天下雨天"
}else if(choice === "snowing"){
para.textContent = "下雪啦下雪啦"
}else{
para.textContent = "";
}
}
结果展示:
3.嵌套if...else
const select = document.querySelector("select");
const para =document.querySelector("p");
select.addEventListener("change",setWeather);
function setWeather(){
const choice = select.value;
let temperature = 90;
if (choice === "sunny") {
if (temperature < 86) {
para.textContent = `外面现在是 ${temperature} 华氏度——风和日丽。`;
} else if (temperature >= 86) {
para.textContent = `外面现在是 ${temperature} 华氏度——很热!`;
}
}
}
结果展示:
可以不给temperature赋值,从控制台输入,随之更改内容,代码可更改为:
const select = document.querySelector("select");
const para =document.querySelector("p");
select.addEventListener("change",setWeather);
let temperature;
function setWeather(){
const choice = select.value;
if (choice === "sunny") {
if (temperature < 86) {
para.textContent = `外面现在是 ${temperature} 华氏度——风和日丽。`;
} else if (temperature >= 86) {
para.textContent = `外面现在是 ${temperature} 华氏度——很热!`;
}
}
}
结果展示:
控制台输入:
选择晴天就会显示以下内容:
4.与 或 非
①&&
——逻辑与。允许你把两个或多个表达式连在一起,这样所有的表达式都必须单独评估为 true
,整个表达式才能返回 true
。
②||
——逻辑或。允许你把两个或多个表达式连在一起,其中一个或多个表达式必须单独评估为 true
,整个表达式才能返回 true
。
1.逻辑与------例子:
const select = document.querySelector("select");
const para =document.querySelector("p");
select.addEventListener("change",setWeather);
let temperature;
function setWeather(){
const choice = select.value;
if (choice === "sunny" && temperature < 86) {
para.textContent =
"外面现在是" +
temperature +
" 华氏度——风和日丽。让我们去海滩或公园,吃个冰激凌。";
} else if (choice === "sunny" && temperature >= 86) {
para.textContent =
"外面现在是" +
temperature +
" 华氏度——很热!如果你想出去,一定要涂抹一些防晒霜。";
}
}
结果显示:
2.逻辑或-----例子:
let iceCreamVanOutside = "on fire";
let houseStatus = "no fire";
if (iceCreamVanOutside || houseStatus === "on fire") {
console.log("你需要赶紧离开这间房子。");
} else {
console.log("或许你应该呆在这里。");
}
结果展示:
③逻辑非 !
运算符可以用于对一个表达式取否。
let iceCreamVanOutside = "on fire";
let houseStatus = "no fire";
if (!(iceCreamVanOutside || houseStatus === 'on fire')) {
console.log('或许你应该呆在这里。');
} else {
console.log("你需要赶紧离开这间房子。");
}
结果展示:
5.Switch语句
html文件中:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<label for="weather">选择今天的天气:</label>
<select id="weather">
<option value="">做出选择</option>
<option value="sunny">晴天</option>
<option value="rainy">雨天</option>
<option value="snowing">下雪天</option>
</select>
<p></p>
<script src="scripts/main1.js" defer></script>
</body>
</html>
js文件中:
const select = document.querySelector("select");
const para = document.querySelector("p");
select.addEventListener("change",setWeather);
function setWeather(){
const choice = select.value;
switch(choice){
case "sunny":para.textContent = "阳光明媚";break;
case "rainy":para.textContent = "下雨天";break;
case "snowing":para.textContent = "下雪啦";break;
default:
para.textContent ="";
}
}
结果展示:
选择 晴天 显示:
6.三元运算符
let isbirthday;
const greeting = isbirthday ? "生日快乐" : "你好";
结果展示:
let isbirthday = true;
const greeting = isbirthday ? "生日快乐" : "你好";
结果展示:
let isbirthday = false;
const greeting = isbirthday ? "生日快乐" : "你好";
结果展示:
三元运算符实例:
html文件中显示:
<label for="theme">选择主题:</label>
<select id="theme">
<option value="white">白</option>
<option value="black">黑</option>
</select>
<h1>你好</h1>
在js文件中显示:
const select = document.querySelector("select");
const html = document.querySelector("html");
document.body.style.padding = "10px";
function update(bgColor, textColor) {
html.style.backgroundColor = bgColor;
html.style.color = textColor;
}
select.addEventListener("change", () =>
select.value === "black"
? update("black", "white")
: update("white", "black"),
);
结果展示: