how2j.cn

关键字 简介 示例代码
$.each()
遍历
示例代码
$.unique()
去除重复
示例代码
$.inArray()
是否存在$.inArray
示例代码
示例 1 : 遍历   
示例 2 : 去除重复   
示例 3 : 是否存在$.inArray   

示例 1 :

遍历

$.each 遍历一个数组
第一个参数是数组
第二个参数是回调函数 i是下标,n是内容
"); window.frames["iframe_show1003"].document.write(decodeHtml($("textarea#stepcodeTextarea1003").val())); window.frames["iframe_show1003"].document.close(); $(window.frames["iframe_show1003"]).load(function(){ $("#iframe_show1003").height($("#iframe_show1003").contents().find("body").height()+showittryitheight); }); $("#iframe_show1003").height($("#iframe_show1003").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script src="http://127.0.0.1/study/jquery.min.js"></script> <script> var a = new Array(1,2,3); $.each( a, function(i, n){ document.write( "元素[" + i + "] : " + n + "<br>" ); }) document.close(); </script>
<script src="http://127.0.0.1/study/jquery.min.js"></script>

<script>
var a = new Array(1,2,3);
$.each( a, function(i, n){
  document.write( "元素[" + i + "] : " + n + "<br>" );
})
document.close();

</script>  

"); window.frames["iframe1003"].document.write(decodeHtml(code1003)); window.frames["iframe1003"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1003"]).load(function(){ $("#iframe1003").height($("#iframe1003").contents().find("body").height()+showittryitheight); }); $("#iframe1003").height($("#iframe1003").contents().find("body").height()+showittryitheight); alreadyWriteCode1003 = code1003; $("#rendering1003").hide(); $("#rendered1003").show(); } var tRereshRetry2DemoPanel1003 = setInterval(rereshRetry2DemoPanel1003,1000); var binded1003 = false; $("textarea#stepcodeTextarea1003").keyup(function(){ if(!binded1003){ $(window).bind('beforeunload',function(){ binded1003 = true; return "xxxx"; }); } var newCode = $(this).val() code1003 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1003!=newCode){ // window.frames["iframe1003"].document.write("
"); // window.frames["iframe1003"].document.write(decodeHtml($("textarea#stepcodeTextarea1003").val())); // window.frames["iframe1003"].document.close(); // $(window.frames["iframe1003"]).load(function(){ // $("#iframe1003").height($("#iframe1003").contents().find("body").height()+showittryitheight); // }); // code1003 = newCode; // } }); $(".tryButton1003").click(function(){ $("#tryDiv1003").show(); $("#stepcodeTextarea1003").focus(); $("#stepcodeTextarea1003").height(200); $("#iframe1003").height(0); window.frames["iframe1003"].document.write("
"); window.frames["iframe1003"].document.write(decodeHtml($("textarea#stepcodeTextarea1003").val())); window.frames["iframe1003"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1003"]).load(function(){ $("#iframe1003").height($("#iframe1003").contents().find("body").height()+showittryitheight); }); $("#iframe1003").height($("#iframe1003").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1003.focus(); editor1003.setSize(null, "250"); $("#rendering1003").hide(); $("#rendered1003").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor1003 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1003"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor1003.on("change",function(doc){ if(!binded1003){ $(window).bind('beforeunload',function(){ binded1003 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1003 = newCode; $("textarea#stepcodeTextarea1003").val(newCode); if(alreadyWriteCode1003!=code1003){ lastModifedTime1003 = new Date().getTime(); $("#rendering1003").show(); $("#rendered1003").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1003 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1003.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1003").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果
示例 2 :

去除重复

$.unique() 去掉重复的元素

注意 : 执行unique之前,要先调用sort对数组的内容进行排序
"); window.frames["iframe_show1004"].document.write(decodeHtml($("textarea#stepcodeTextarea1004").val())); window.frames["iframe_show1004"].document.close(); $(window.frames["iframe_show1004"]).load(function(){ $("#iframe_show1004").height($("#iframe_show1004").contents().find("body").height()+showittryitheight); }); $("#iframe_show1004").height($("#iframe_show1004").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script src="http://127.0.0.1/study/jquery.min.js"></script> <script> var a = new Array(5,2,4,2,3,3,1,4,2,5); a.sort(); $.unique(a); $.each( a, function(i, n){ document.write( "元素[" + i + "] : " + n + "<br>" ); }) document.close(); </script>
<script src="http://127.0.0.1/study/jquery.min.js"></script>
 
<script>
var a = new Array(5,2,4,2,3,3,1,4,2,5);
a.sort();
$.unique(a);
$.each( a, function(i, n){
  document.write( "元素[" + i + "] : " + n + "<br>" );
})
document.close();
 
</script>  
"); window.frames["iframe1004"].document.write(decodeHtml(code1004)); window.frames["iframe1004"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1004"]).load(function(){ $("#iframe1004").height($("#iframe1004").contents().find("body").height()+showittryitheight); }); $("#iframe1004").height($("#iframe1004").contents().find("body").height()+showittryitheight); alreadyWriteCode1004 = code1004; $("#rendering1004").hide(); $("#rendered1004").show(); } var tRereshRetry2DemoPanel1004 = setInterval(rereshRetry2DemoPanel1004,1000); var binded1004 = false; $("textarea#stepcodeTextarea1004").keyup(function(){ if(!binded1004){ $(window).bind('beforeunload',function(){ binded1004 = true; return "xxxx"; }); } var newCode = $(this).val() code1004 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1004!=newCode){ // window.frames["iframe1004"].document.write("
"); // window.frames["iframe1004"].document.write(decodeHtml($("textarea#stepcodeTextarea1004").val())); // window.frames["iframe1004"].document.close(); // $(window.frames["iframe1004"]).load(function(){ // $("#iframe1004").height($("#iframe1004").contents().find("body").height()+showittryitheight); // }); // code1004 = newCode; // } }); $(".tryButton1004").click(function(){ $("#tryDiv1004").show(); $("#stepcodeTextarea1004").focus(); $("#stepcodeTextarea1004").height(200); $("#iframe1004").height(0); window.frames["iframe1004"].document.write("
"); window.frames["iframe1004"].document.write(decodeHtml($("textarea#stepcodeTextarea1004").val())); window.frames["iframe1004"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1004"]).load(function(){ $("#iframe1004").height($("#iframe1004").contents().find("body").height()+showittryitheight); }); $("#iframe1004").height($("#iframe1004").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1004.focus(); editor1004.setSize(null, "250"); $("#rendering1004").hide(); $("#rendered1004").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor1004 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1004"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor1004.on("change",function(doc){ if(!binded1004){ $(window).bind('beforeunload',function(){ binded1004 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1004 = newCode; $("textarea#stepcodeTextarea1004").val(newCode); if(alreadyWriteCode1004!=code1004){ lastModifedTime1004 = new Date().getTime(); $("#rendering1004").show(); $("#rendered1004").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1004 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1004.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1004").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果
示例 3 :

是否存在$.inArray

$.inArray 返回元素在数组中的位置 ,如果不存在返回-1
"); window.frames["iframe_show1005"].document.write(decodeHtml($("textarea#stepcodeTextarea1005").val())); window.frames["iframe_show1005"].document.close(); $(window.frames["iframe_show1005"]).load(function(){ $("#iframe_show1005").height($("#iframe_show1005").contents().find("body").height()+showittryitheight); }); $("#iframe_show1005").height($("#iframe_show1005").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script src="http://127.0.0.1/study/jquery.min.js"></script> <script> var a = new Array(1,2,3,4,5,6,7,8); document.write($.inArray(9,a)); document.close(); </script>
<script src="http://127.0.0.1/study/jquery.min.js"></script>
 
<script>
var a = new Array(1,2,3,4,5,6,7,8);
document.write($.inArray(9,a));
document.close();
 
</script>  
"); window.frames["iframe1005"].document.write(decodeHtml(code1005)); window.frames["iframe1005"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1005"]).load(function(){ $("#iframe1005").height($("#iframe1005").contents().find("body").height()+showittryitheight); }); $("#iframe1005").height($("#iframe1005").contents().find("body").height()+showittryitheight); alreadyWriteCode1005 = code1005; $("#rendering1005").hide(); $("#rendered1005").show(); } var tRereshRetry2DemoPanel1005 = setInterval(rereshRetry2DemoPanel1005,1000); var binded1005 = false; $("textarea#stepcodeTextarea1005").keyup(function(){ if(!binded1005){ $(window).bind('beforeunload',function(){ binded1005 = true; return "xxxx"; }); } var newCode = $(this).val() code1005 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1005!=newCode){ // window.frames["iframe1005"].document.write("
"); // window.frames["iframe1005"].document.write(decodeHtml($("textarea#stepcodeTextarea1005").val())); // window.frames["iframe1005"].document.close(); // $(window.frames["iframe1005"]).load(function(){ // $("#iframe1005").height($("#iframe1005").contents().find("body").height()+showittryitheight); // }); // code1005 = newCode; // } }); $(".tryButton1005").click(function(){ $("#tryDiv1005").show(); $("#stepcodeTextarea1005").focus(); $("#stepcodeTextarea1005").height(200); $("#iframe1005").height(0); window.frames["iframe1005"].document.write("
"); window.frames["iframe1005"].document.write(decodeHtml($("textarea#stepcodeTextarea1005").val())); window.frames["iframe1005"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1005"]).load(function(){ $("#iframe1005").height($("#iframe1005").contents().find("body").height()+showittryitheight); }); $("#iframe1005").height($("#iframe1005").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1005.focus(); editor1005.setSize(null, "250"); $("#rendering1005").hide(); $("#rendered1005").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor1005 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1005"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor1005.on("change",function(doc){ if(!binded1005){ $(window).bind('beforeunload',function(){ binded1005 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1005 = newCode; $("textarea#stepcodeTextarea1005").val(newCode); if(alreadyWriteCode1005!=code1005){ lastModifedTime1005 = new Date().getTime(); $("#rendering1005").show(); $("#rendered1005").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1005 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1005.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1005").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果


HOW2J公众号,关注后实时获知布最新的教程和优惠活动,谢谢。


关于 前端部分-JQuery-数组操作 的提问

尽量提供截图代码异常信息,有助于分析和解决问题。 也可进本站QQ群交流: 620943819
提问尽量提供完整的代码,环境描述,越是有利于问题的重现,您的问题越能更快得到解答。
对教程中代码有疑问,请提供是哪个步骤,哪一行有疑问,这样便于快速定位问题,提高问题得到解答的速度
在已经存在的几千个提问里,有相当大的比例,是因为使用了和站长不同版本的开发环境导致的,比如 jdk, eclpise, idea, mysql,tomcat 等等软件的版本不一致。
请使用和站长一样的版本,可以节约自己大量的学习时间。 站长把教学中用的软件版本整理了,都统一放在了这里, 方便大家下载: http://how2j.cn/k/helloworld/helloworld-version/1718.html

上传截图