三目运算符能使我们的代码更为简洁,因而包括小编的我也很是青睐它,不过有时候我们给予它更多的希望,小编处于学习阶段,先从笔记开始:

 (3>1)?console.log(1):console.log(2);//

 //expression?expression1:expression2

3>1为true吗?为true的是就执行expression1,否则就执行expression2;

三目嵌套:

 (5>4)?alert(1):((2>1)?alert(2):((4>5)?alert(0):alert(9)));

三目嵌套。表达式1,5>4吗?大于就alert(1),否则就执行表达式2 ,2>1吗?大于就alert(2),否则就执行表达式3,4>5吗?大于就alert(0),否则就alert(9);

来个三目小demo:

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>三目运算符的运用</title>
<script src="https://cdn.bootcss.com/jquery/2.2.3/jquery.js"></script>
</head>
<style>
.red{
background-color: red;
}
.green{
background-color: green;
}
.red:hover{
background-color: orange;
}
.green:hover{
background-color: cyan;
}
</style>
<body>
<button class="close">关闭</button>
<button class="open">开启</button>
</body>
<script>
$('button').click(function(){
($(this).html()=='关闭')?$(this).html('开启').addClass('green').removeClass('red'):$(this).html('关闭').addClass('red').removeClass('green');
})
</script>
</html>

有时候我们有这样的需求,点击一个按钮,给按钮加一个类,同时删除一个类,同时还要在这个元素上变化很多,为了逻辑清晰,最好还是用if()else()

三目强化:多个值的改变 json方式 必须先声明 否则会报undfined

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>三目运算符的运用</title>
<script src="https://cdn.bootcss.com/jquery/2.2.3/jquery.js"></script>
</head>
<style>
.red{
background-color: red;
}
.green{
background-color: green;
}
</style>
<body>
<button class="close">关闭</button>
<span id="box">这是一段小飞写的文字...</span>
</body>
<script>
$('button').click(function(){
var obj={status:'',color:'',size:''};
($(this).html()=='关闭')?obj={font:'开启',color:'green',size:'20px'}:obj={font:'关闭',color:'red',size:'12px'};
$('#box').css({
color:obj.color,
fontSize:obj.size
});
$(this).html(obj.font); })
</script>
</html>

说下这个需求,点击按钮切换span标签里的内容的字体大小和字体颜色,同时切换按钮里的内容。

三目双重判断:个人喜好这么叫


 $(window).scroll(function(event){
$('#box')[$(window).scrollTop()>300?"fadeIn":"fadeOut"]($(window).scrollTop()>300?300:500);
console.log($(window).scrollTop());
});

 

关于javascript三目的更多相关文章

  1. ng-class结合三目运算

    ng-class文档:https://docs.angularjs.org/api/ng/directive/ngClass 但是在实际项目中可能会用到三目运算,实例如下: <ul> &l ...

  2. angularjs的三目运算

    前言:前几天写代码的时候遇到一个问题,有一个按钮,有"已关注"和"+关注"两种状态,需要对这两种状态的按钮的背景颜色进行区分,单后点击"已关注&quo ...

  3. 【Python全栈笔记】03 [模块二] 16-17 Oct Set 集合,三目运算

    Set 集合 set - unordered collections of unique elements 创建一个set/一个空set # create a new set set1 = {1,2, ...

  4. python函数,lambda表达式,三目运算,列表解析,递归

    一.自定义函数 定义函数时,函数体不执行:只有在调用函数时,函数体才执行.函数的结构: 1. def 2. 函数名 3. 函数体 def func_name(): 函数体 4. 返回值 如果没有声明返 ...

  5. JavaScript 三种绑定事件方式之间的区别

    JavaScript三种绑定事件的方式: 1. <div id="btn" onclick="clickone()"></div> // ...

  6. 从头开始学JavaScript (三)——数据类型

    原文:从头开始学JavaScript (三)--数据类型 一.分类 基本数据类型:undefined.null.string.Boolean.number 复杂数据类型:object object的属 ...

  7. 三目运算的使用&bytes类型转str类型

    一.三目运算的使用 就像c语言中有三目运算符一样,python中也有三目运算符,废话不多说直接上代码 a=3 c=4 b=a if a>c else c print(b) 意思就和 if a&g ...

  8. 三目运算:and/or 技巧

    三目运算:and/or 技巧 and, or 联合起来有个小技巧: print 2 < 3 and True or False 说明:     如果  2 小于 3 了,则输出 True ,  ...

  9. js switch判断 三目运算 while 及 属性操作

    三 目运算:如var a = 10: var b= 12: c = a>b ?a:b; 若成立执行a否则执行b var isHide = true; 若用if判断语句如下 if(isHide) ...

随机推荐

  1. skipper prometheus 监控

    skipper 是支持prometheus监控的,只是没有启用,需要添加参数 -enable-prometheus-metrics 测试使用的是一个简单nginx web ,同时使用docker-co ...

  2. GCC related commands

    GCC   -l  option is to link the library. It can use for static and share link.  Link -l with library ...

  3. GBDT(Gradient Boosting Decision Tree) 没有实现仅仅有原理

                阿弥陀佛.好久没写文章,实在是受不了了.特来填坑,近期实习了(ting)解(shuo)到(le)非常多工业界经常使用的算法.诸如GBDT,CRF,topic model的一些算 ...

  4. C# to IL 12 Arrays(数组)

    An array is a contiguous block of memory that stores values of the same type. These valuesare an ind ...

  5. Survival Shooter 学习

    using UnityEngine; using System.Collections; namespace CompleteProject { /// <summary> /// 摄像机 ...

  6. MySQL全文本搜索

    启用全文本搜索支持 create table text( -> id int not null auto_increment, -> texts text null, -> prim ...

  7. Revit api 创建楼梯图元

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  8. bzoj 2806 [Ctsc2012]Cheat——广义后缀自动机+单调队列优化DP

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2806 只想着怎么用后缀数据结构做,其实应该考虑结合其他算法. 可以二分那个长度 L .设当前 ...

  9. Redis:高性能文件缓存key-value储存

    1.前言 a.Redis是一个开源,先进的key-value(键/值对)存储,并且勇于构建高性能,可扩展的Web应用程序的完美解决方案 b.Redis和Memcached的对比 b.1 Redis数据 ...

  10. Centos7 环境下 Python2.7 换成 Python3.7 运行 scrapy 应用所遇到的问题记录

    参考网友的安装过程 Linux系统Centos安装Python3.7 设置Python默认为Python3.7 mv /usr/bin/python /usr/bin/python.bak ln -s ...