1.介紹了JS數組合并push與concat區別,結合實例形式分析了javaScript中針對數組合并操作使用push與concat的區別,需要的朋友可以參考下
var arr = [];arr.push(1);arr.push([2, 3]);arr.push(4, 5);arr = arr.concat(6);arr = arr.concat([7, 8]);arr = arr.concat(9, 10);arr.each(function(index, value){ alert(value);});結果
12,345678910差異 push 遇到數組參數時,把整個數組參數作為一個元素;而 concat 則是拆開數組參數,一個元素一個元素地加進去。 push 直接改變當前數組;concat 不改變當前數組。
3.Ajax文件下載
<script type="text/Javascript"> function DownLoad(strUrl) { var form = $("<form>"); //定義一個form表單 form.attr('style', 'display:none'); //在form表單中添加查詢參數 form.attr('target', ''); form.attr('method', 'post'); form.attr('action', "/QuestionInfo/DowmLoad"); var input1 = $('<input>'); input1.attr('type', 'hidden'); input1.attr('name', 'strUrl'); input1.attr('value', strUrl); $('body').append(form); //將表單放置在web中 form.append(input1); //將查詢參數控件提交到表單上 form.submit(); } </script>后臺代碼 #region 文檔下載 /// <summary> /// 文件下載函數 /// </summary> /// <param name="fileUrl"></param> /// <returns></returns> [HttpPost] public void DowmLoad(string strUrl) { try { string fullPathUrl = Server.MapPath(strUrl);//獲取下載文件的路勁 System.IO.FileInfo file = new System.IO.FileInfo(fullPathUrl); if (file.Exists)//判斷文件是否存在 { Response.Clear(); Response.ClearHeaders(); Response.Buffer = false; Response.AddHeader("content-disposition", "attachment;filename=" + file.Name); Response.AddHeader("cintent_length", "attachment;filename=" + HttpUtility.UrlDecode(file.Name)); Response.AddHeader("cintent_length", file.Length.ToString()); Response.ContentType = "application/octet-stream"; Response.WriteFile(file.FullName);//通過response對象,執行下載操作 Response.Flush(); Response.End(); } } catch(Exception e) { Console.Write(e.ToString()); } }4.jQuery 遍歷函數包括了用于篩選、查找和串聯元素的方法。
復制代碼 var arr = [ "one", "two", "three", "four"]; $.each(arr, function(){ alert(this); }); //上面這個each輸出的結果分別為:one,two,three,four var arr1 = [[1, 4, 3], [4, 6, 6], [7, 20, 9]] $.each(arr1, function(i, item){ alert(item[0]); }); //其實arr1為一個二維數組,item相當于取每一個一維數組, //item[0]相對于取每一個一維數組里的第一個值 //所以上面這個each輸出分別為:1 4 7 var obj = { one:1, two:2, three:3, four:4}; $.each(obj, function(i) { alert(obj[i]); }); //這個each就有更厲害了,能循環每一個屬性 //輸出結果為:1 2 3 4<html><head><script type="text/javascript" src="/jquery/jquery.js"></script><script type="text/javascript">$(document).ready(function(){ $("button").click(function(){ $("li").each(function(){ alert($(this).text()) }); });});</script></head><body><button>輸出每個列表項的值</button><ul><li>Coffee</li><li>Milk</li><li>Soda</li></ul></body></html>$(function(){ var arr = []; $(":checkbox").each(function(index){ arr.push(this.id); }); var str = arr.join(","); alert(str);})$(function(){ var str = $(":checkbox").map(function() { return this.id; }).get().join(); alert(str);})JS函數的參數(arguments)的使用
function Test(a, b){var i, s = "Test函數有";var numargs = arguments.length; // 獲取實際被傳遞參數的數值。var expargs = Test.length; // 獲取期望參數的數值,函數定義時的預期參數個數(有a和b 2個參數)。s += (expargs + "個參數。");s += "/n/n"for (i =0 ; i < numargs; i++){ // 獲取參數內容。s += " 第" + i + "個參數是:" + arguments[i] + "/n";}return(s); // 返回參數列表。}alert(Test('param1','second param','第三個參數'));<html> <head> <script language="javascript"> function reloadList(){ if(typeof arguments[0] == "function"){ arguments[0].call(this); arguments[0](); } if(typeof arguments[0] == "string") alert(arguments[0]); if(typeof arguments[0] == "number") alert(arguments[0]); if(typeof arguments[0] == "undefined") alert(arguments[0]); if(typeof arguments[0] == "boolean") alert(arguments[0]); if(typeof arguments[0] == "null") alert(arguments[0]); } reloadList(function(){}); </script> </head> <body> </body>新聞熱點
疑難解答