push和shift的区别
push方法有一个参数,也就是要添加到数组尾部的元素代码<script type="text/javascript"> var arr = ["HTML","CSS","JavaScript","DOM"]; var arr2 = [1,2,3,4]; arr.push("PHP"); var popped = arr2.pop();</script>shift方法则没有参数,而是返回从数组头部删除的元素。
代码<script type="text/javascript"> var arr = ["HTML","CSS","JavaScript","DOM"]; var arr2 = [1,2,3,4]; arr.unshift("PHP"); var shifted = arr2.shift();</script>什么是队列
队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表。进行插入操作的端称为队尾,进行删除操作的端称为队头。队列的数据元素又称为队列元素。在队列中插入一个队列元素称为入队,从队列中删除一个队列元素称为出队。因为队列只允许在一端插入,在另一端删除,所以只有最早进入队列的元素才能最先从队列中删除,故队列又称为先进先出(FIFO—first in first out)线性表。什么是栈
栈是限定仅在表头进行插入和删除操作的线性表。栈作为一种数据结构,是一种只能在一端进行插入和删除操作的特殊线性表。它按照先进后出的原则存储数据,先进入的数据被压入栈底,最后的数据在栈顶,需要读数据的时候从栈顶开始弹出数据(最后一个数据被第一个读出来)。栈具有记忆作用,对栈的插入与删除操作中,不需要改变栈底指针。栈是允许在同一端进行插入和删除操作的特殊线性表。允许进行插入和删除操作的一端称为栈顶(top),另一端为栈底(bottom);栈底固定,而栈顶浮动;栈中元素个数为零时称为空栈。插入一般称为进栈(PUSH),删除则称为退栈(POP)。栈也称为后进先出表。栈可以用来在函数调用的时候存储断点,做递归时要用到栈!堆栈帧一般包含如下几方面的信息:1.函数的返回地址和参数2. 临时变量:包括函数的非静态局部变量以及编译器自动生成的其他临时变量。sort的底层原理
正序<script>
var numArr = new Array(12,23,1,4,23,34,2,5); numArr.sort(function compare(a,b){return a-b;}); for (var i = 0; i<numArr.length; i++) { document.write(numArr[i] +"<br>"); } </script>倒序<script>
var numArr = new Array(12,23,1,4,23,34,2,5); numArr.sort(function compare(a,b){return b-a;}); for (var i = 0; i<numArr.length; i++) { document.write(numArr[i] +"<br>"); } </script>split用法
split() 方法用于把一个字符串分割成字符串数组。<script type="text/javascript">var str="How are you doing today?"
document.write(str.split(" ") + "<br />")
document.write(str.split("") + "<br />")document.write(str.split(" ",3))</script>
找验证码的插件
html代码:<script type="text/javascript" src="idcode/jquery.min.js"></script>
<script language="javascript" src="idcode/jquery.idcode.js"></script><link type="text/css" rel="stylesheet" href="idcode/jquery.idcode.css" /><script>$(document).ready(function(){ $.idcode.setCode();});</script><span id="idcode"></span>css代码:#ehong-code-input{
width:42px; letter-spacing:2px; margin:0px 8px 0px 0px;}.ehong-idcode-val{ position:relative; padding:1px 4px 1px 4px; top:0px; *top:-3px; letter-spacing:4px; display:inline; cursor:pointer; font-size:16px; font-family:"Courier New", Courier, monospace; text-decoration:none; font-weight:bold;}.ehong-idcode-val0{ border:solid 1px #A4CDED; background-color:#ECFAFB;}.ehong-idcode-val1{
border:solid 1px #A4CDED; background-color:#FCEFCF;}.ehong-idcode-val2{ border:solid 1px #6C9; background-color:#D0F0DF;}.ehong-idcode-val3{ border:solid 1px #6C9; background-color:#DCDDD8;}.ehong-idcode-val4{ border:solid 1px #6C9; background-color:#F1DEFF;}.ehong-idcode-val5{ border:solid 1px #6C9; background-color:#ACE1F1;}.ehong-code-val-tip{ font-size:12px; color:#1098EC; top:0px; *top:-3px; position:relative; margin:0px 0px 0px 4px; cursor:pointer;}js代码:(function($){
var settings = { e : 'idcode', codeType : { name : 'follow', len: 4}, codeTip : '看不清,换一组', inputID : '' };var _set = {
storeLable : 'codeval', store : '#ehong-code-input', codeval : '#ehong-code' } $.idcode = { getCode:function(option){ _commSetting(option); return _storeData(_set.storeLable, null); }, setCode:function(option){ _commSetting(option); _setCodeStyle("#"+settings.e, settings.codeType.name, settings.codeType.len);},
validateCode:function(option){ _commSetting(option); var inputV; if(settings.inputID){ inputV=$('#' + settings.inputID).val(); }else{ inputV=$(_set.store).val(); }if(inputV == _storeData(_set.storeLable, null)){
return true; }else{ _setCodeStyle("#"+settings.e, settings.codeType.name, settings.codeType.len); return false; } } };function _commSetting(option){
$.extend(settings, option); }function _storeData(dataLabel, data){
var store = $(_set.codeval).get(0); if(data){ $.data(store, dataLabel, data); }else{ return $.data(store, dataLabel); } }function _setCodeStyle(eid, codeType, codeLength){
var codeObj = _createCode(settings.codeType.name, settings.codeType.len); var randNum = Math.floor(Math.random()*6); var htmlCode='' if(!settings.inputID){ htmlCode='<span><input id="ehong-code-input" type="text" maxlength="4" /></span>'; } htmlCode+='<div id="ehong-code" class="ehong-idcode-val ehong-idcode-val'; htmlCode+=String(randNum); htmlCode+='" href="#" οnblur="return false" οnfοcus="return false" οncοntextmenu="return false" οnclick="$.idcode.setCode()">' + _setStyle(codeObj) + '</div>' + '<span id="ehong-code-tip-ck" class="ehong-code-val-tip" οnclick="$.idcode.setCode()">'+ settings.codeTip +'</span>'; $(eid).html(htmlCode); _storeData(_set.storeLable, codeObj); }function _setStyle(codeObj){
var fnCodeObj = new Array(); var col = new Array('#BF0C43', '#E69A2A','#707F02','#18975F','#BC3087','#73C841','#780320','#90719B','#1F72D8','#D6A03C','#6B486E','#243F5F','#16BDB5'); var charIndex; for(var i=0;i<codeObj.length;i++){ charIndex = Math.floor(Math.random()*col.length); fnCodeObj.push('<font color="' + col[charIndex] + '">' + codeObj.charAt(i) + '</font>'); } return fnCodeObj.join(''); } function _createCode(codeType, codeLength){ var codeObj; if(codeType=='follow'){ codeObj = _createCodeFollow(codeLength); }else if(codeType=='calc'){ codeObj = _createCodeCalc(codeLength); }else{ codeObj=""; } return codeObj; }function _createCodeCalc(codeLength){
var code1, code2, codeResult; var selectChar = new Array('0','1','2','3','4','5','6','7','8','9'); var charIndex; for(var i=0;i<codeLength;i++){ charIndex = Math.floor(Math.random()*selectChar.length); code1 +=selectChar[charIndex];charIndex = Math.floor(Math.random()*selectChar.length);
code2 +=selectChar[charIndex]; } return [parseInt(code1), parseInt(code2) , parseInt(code1) + parseInt(code2)] ; }function _createCodeFollow(codeLength){
var code = ""; var selectChar = new Array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');for(var i=0;i<codeLength;i++){
var charIndex = Math.floor(Math.random()*selectChar.length); if(charIndex % 2 == 0){ code+=selectChar[charIndex].toLowerCase(); }else{ code +=selectChar[charIndex]; } } return code; }})(jQuery);