`

第14章 工具函数(上)

 
阅读更多

index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>工具函数</title>
<script type="text/javascript" src="jquery-1.10.1.js"></script>
<script type="text/javascript" src="demo.js"></script>
<link type="text/css" rel="stylesheet" href="style.css" />
</head>
<body>
<!--
<div></div>
<div></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div></div>
-->
<ul>
	<li>1</li>
    <li>2</li>
    <li>3</li>
</ul>
</body>
</html>

 

demo.js

$(function(){
	/*
	var str = '            jQuery               ';
	alert(str);
	alert($.trim(str));
	
	var arr = ['张三','李四','王五','马六'];
	$.each(arr,function(index,value){
		$('#box').html($('#box').html()+(index+1)+'.'+value+'<br/>');
	});
	
	$.each($.ajax(),function(name,fn){
		$('#box').html($('#box').html()+fn+'<br/><br/>');
	});
	
	var arr = [4,2,6,9,11,25,38,59];
	var arrGrep = $.grep(arr,function(element,index){
		return index<5 && element<6;//这里按道理是布尔值,但整体返回一个数值
	});
	alert(arrGrep);
	
	var arr = [4,2,6,9,11,25,38,59];
	var arrMap = $.map(arr,function(element,index){
		//return index<5 && element<6;//这里就是按布尔值返回的
		//return element+1;
		if(index<5&&element<6){
			return element;
		}
	});
	alert(arrMap);
	
	var arr = [4,2,6,9,11,25,38,59];
	alert($.inArray(11,arr));
	
	var arr = [4,2,6,9,11,25,38,59];
	var arr2 = [200,300];
	alert($.merge(arr,arr2));
	
	var arr = [4,2,6,9,11,25,38,59];
	var arr2 = [2,9,4];
	var arr3 = $.merge(arr,arr2);
	alert($.unique(arr3));
	
	var divs = $('div').get();
	divs = divs.concat($('.box').get());
	//alert(divs.length);
	alert($(divs).size());
	$.unique(divs);
	alert($(divs).size());
	*/
	
	alert($('li').toArray().length);
	alert($($('li').toArray()).size());

});

 

 

 

 

 

2
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics