JavaScript-------条件语句

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"),
);

结果展示:

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值