[jQuery]Utilities--join,grep (用於陣列)

[jQuery]Utilities--join,grep (用於陣列)

語法:

  • join:陣列每個元素中間加入指定的字串
    • [陣列].join('要插入的符號');
  • grep:用於陣列的篩選
    • $.grep(array, function([當前元素],[index]){})

範例:

<HTML>
<HEAD>
<script type="Text/JavaScript" src="js/jQuery.js"></script>
<script type="Text/JavaScript">
$(document).ready(function(){
	var nums=[1,9,3,8,6,1,5,9,4,7,3,8,6,9,1];
	$('#btn1').click(function(){
		//$('#div1').append('<p>' + nums.join('|') + '</p>');
		$('#div1').append('<p>' + nums.join($('#txt1').val()) + '</p>');
	});
	$('#btn2').click(function(){
		var arr=$.grep(nums,function(n,i){
			return(n>=4&&n!=5)
		});
		$('#div1').append('<p>' + arr.join(',') + '</p>');
	});
})

</script>
<style>
	.d1{
		background-color:yellow;
		color:blue;
		font-size:18px;
	}
</style>
</HEAD>
<BODY>
<FORM action="" method=POST id=form1 name=form1>
1,9,3,8,6,1,5,9,4,7,3,8,6,9,1<br>
<input type="text" id="txt1" name="txt1" value=""><br>
<input type="button" id="btn1" name="btn1" value="示範join" />
<input type="button" id="btn2" name="btn2" value="示範grep,取大於等於4且不等於5">
<hr />
<div id="div1">我是div1</div>
</FORM>
</BODY>
</HTML>

 


以下是簽名:


Microsoft MVP
Visual Studio and Development Technologies
(2005~Now)