码迷,mamicode.com
首页 > Web开发 > 详细

JQuery 三元运算

时间:2017-05-02 16:02:12      阅读:607      评论:0      收藏:0      [点我收藏+]

标签:jquery

JQuery三元运算语法:

var value = 条件?value1:value2;

解释:设置一个变量value,根据条件进行判断,如果条件为真,则设置value=value1,否则value=value2


实例:

<script src="js/jquery-3.1.1.js"></script>

<script type="text/javascript">
	function checkAll(){
		$(‘ :checkbox‘).prop(‘checked‘,true);
	}
	function cancleall(){
		$(‘ :checkbox‘).prop(‘checked‘,false);
	}
	
</script>
<script type="text/javascript">
	function reverseAll(){
		$(‘ :checkbox‘).each(function(){
			// if(this.checked){
			// 	this.checked = false;
			// }else{
			// 	this.checked = true;
			// }

			// if($(this).prop(‘checked‘)){
			// 	$(this).prop(‘checked‘,false);

			// }else{
			// 	$(this).prop(‘checked‘,true);
			// }
			var v = $(this).prop(‘checked‘)?false:true;
			$(this).prop(‘checked‘,v);
		})
	}	
</script>

prop属性:

$(this).prop(‘checked‘):表示获取checked属性的值,

$(this).prop(‘checked‘,true/false):表示设置checked属性的值为true/false。

prop常用语checked和selected属性

this: 表示DOM对象,$(this)表示JQuery对象,只有JQuery对象才能使用JQuery方法。



本文出自 “zengestudy” 博客,请务必保留此出处http://zengestudy.blog.51cto.com/1702365/1921115

JQuery 三元运算

标签:jquery

原文地址:http://zengestudy.blog.51cto.com/1702365/1921115

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!