js 中的条件语句
if …else语句
var name = '';
if(name === 'zhangsan'){
alert('这是张三');
}else{
console.log('这不是张三')
}
if …elseif…else语句
="weather">
var select = document.querySelector('select');
var para = document.querySelector('p');
select.addEventListener('change', setWeather);
function setWeather() {
var choice = select.value;
if (choice === 'sunny') {
para.textContent = 'It is nice and sunny outside today. Wear shorts! Go to the beach, or the park, and get an ice cream.';
} else if (choice === 'rainy') {
para.textContent = 'Rain is falling outside;
take a rain coat and a brolly, and don\'t stay out for too long.';
} else if (choice === 'snowing') {
para.textContent = 'The snow is coming down — it is freezing! Best to stay in with a cup of hot chocolate, or go build a snowman.';
} else if (choice === 'overcast') {
para.textContent = 'It isn\'t raining, but the sky is grey and gloomy;
it could turn any minute, so take a rain coat just in case.';
} else {
para.textContent = '';
}
}
switch语句
="weather">
var select = document.querySelector('select');
var para = document.querySelector('p');
select.addEventListener('change', setWeather);
function setWeather() {
var choice = select.value;
switch (choice) {
case 'sunny':
para.textContent = 'It is nice and sunny outside today. Wear shorts! Go to the beach, or the park, and get an ice cream.';
break;
case 'rainy':
para.textContent = 'Rain is falling outside;
take a rain coat and a brolly, and don\'t stay out for too long.';
break;
case 'snowing':
para.textContent = 'The snow is coming down — it is freezing! Best to stay in with a cup of hot chocolate, or go build a snowman.';
break;
case 'overcast':
para.textContent = 'It isn\'t raining, but the sky is grey and gloomy;
it could turn any minute, so take a rain coat just in case.';
break;
default:
para.textContent = '';
}
}
三元运算符
var greeting = (isBirthday)?'Happy birthday Mrs.Xiao': 'Today isnot my birthday';
--------------------------
="theme">
This is my websitevar select = document.querySelector('select');
var html = document.querySelector('html');
document.body.style.padding = '10px';
function update(bgColor, textColor) {
html.style.backgroundColor = bgColor;
html.style.color = textColor;
}select.onchange = function() {
( select.value =https://www.it610.com/article/=='black' ) ? update('black','white') : update('white','black');
}
js逻辑运行符
||//或 :表示或者,满足一个条件就可以执行。
&&//与 :表示并且,所有条件全部满足的时候执行。
!//非 : 表示非 ,对一个逻辑取相反的效果执行。
推荐阅读
- 热闹中的孤独
- JS中的各种宽高度定义及其应用
- 我眼中的佛系经纪人
- 《魔法科高中的劣等生》第26卷(Invasion篇)发售
- Android中的AES加密-下
- 放下心中的偶像包袱吧
- C语言字符函数中的isalnum()和iscntrl()你都知道吗
- C语言浮点函数中的modf和fmod详解
- C语言中的时间函数clock()和time()你都了解吗
- (七)谈条件