½¨Õ¾Ñ§ - ÇáËɽ¨Õ¾´Ó´Ë¿ªÊ¼£¡

½¨Õ¾Ñ§-¸öÈ˽¨Õ¾Ö¸ÄÏ,ÍøÒ³ÖÆ×÷,ÍøÕ¾Éè¼Æ,ÍøÕ¾ÖÆ×÷½Ì³Ì

ÍøÒ³ÖÆ×÷Ö®JavascriptʹÓü¼ÇÉ

ʱ¼ä:2009-05-10 21:01À´Ô´: ×÷Õß: µã»÷: ´Î
²ÉÓøüΪ¼ò¶ÌµÄJavascriptд·¨£¬²»½ö¿ÉÒÔ¼õÉÙÊäÈëµÄ×Ö·ûÊý£¬»¹¿ÉÒÔ¼õÉÙÎļþ´óС¡£´ó²¿·Ö²ÉÓüòµ¥Ð´·¨µÄ´úÂ룬ִÐÐЧÂʶ¼ÓÐÇá΢Ìá¸ß¡£

Ò»¡¢¼ò»¯´úÂë

²ÉÓøüΪ¼ò¶ÌµÄJavascriptд·¨£¬²»½ö¿ÉÒÔ¼õÉÙÊäÈëµÄ×Ö·ûÊý£¬»¹¿ÉÒÔ¼õÉÙÎļþ´óС¡£´ó²¿·Ö²ÉÓüòµ¥Ð´·¨µÄ´úÂ룬ִÐÐЧÂʶ¼ÓÐÇá΢Ìá¸ß¡£

1.1  ¼ò»¯³£ÓöÔÏó¶¨Ò壺

ʹÓà var obj = {}; ´úÌæ var obj = new Object();
ʹÓà var arr = []; ´úÌæ var arr = new Array();

1.2  ¾«¼òifÓï¾ä

ÈýÔª²Ù×÷·û¿ÉÒÔÓÐЧ¾«¼òÖ»Éæ¼°¸³Öµ´«Öµ²Ù×÷µÄifÓï¾ä£¬±ÈÈç
var score = 60, grade;
if (score < 60) {
    grade = “²»¼°¸ñ”;
} else {
    grade = “¼°¸ñ”;
}
¿ÉÒÔ¾«¼òΪ£º
var score = 60;
var grade = score < 60 ? “²»¼°¸ñ” : “¼°¸ñ”;
ÈýÔª²Ù×÷·ûÒ²Ö§³ÖǶÌ×£¬µ«ÊÇǶÌ׵IJã´ÎÌ«¶à»áÓ°Ïì³ÌÐòµÄ¿É¶ÁÐÔ£¬Õâ·½ÃæÒª¶à¼ÓÕå×á£

1.3  ʹÓÃJSON

JSONÊÇÒ»ÖÖÇáÁ¿¼¶µÄÊý¾Ý¸ñʽ£¬ÇáÁ¿¼¶Ê×ÏÈÌåÏÖÔÚËüµÄ½á¹¹¶¨Òå·Ç³£¼òµ¥¡£
var obj = {};
obj.p1 = ‘a’;
obj.p2 = ‘b’;
obj.p3 = ‘c’;
¿É¾«¼òΪ£º
var obj = {
    p1 : ‘a’,
    p2 : ‘b’,
    p3 : ‘c’
};

¶þ¡¢Ê¹ÓøßЧÂʵĴúÂë

ÍøÉÏÁ÷´«µÄЧÂÊÓÅ»¯ÎÄÕ·dz£¶à£¬Ò»Ð©±È½ÏרҵµÄJavascriptÊ鼮Ҳ̸µ½Á˲»ÉÙ£¬Òò´Ë£¬ÕâÀï¾ÍÖ»ÁгöһЩºÜÉÙ̸µ½µÄ¡£

2.1  ¾«¼òÑ­»·Ìå

Ñ­»·µÄЧÂʺܴó³Ì¶ÈÉÏÊÇÓÉÑ­»·Ìå¾ö¶¨µÄ£¬ÓëÖ®Ïà±È£¬ÓÃfor»¹ÊÇwhileµÄ²î±ð¾Í̫СÁË¡£¿¼ÂÇÈçϵĴúÂ룬Æ书ÄÜÊÇΪijһÅúÔªËØÌí¼Óʼþ£º
function addEvent(elems, eventName, handler) {
    for (var i = 0, len = elems.length; i < len; i++) {
        if (window.attachEvent) {
            elems[i].attachEvent(”on” + eventName, handler);
        } else if (window.addEventListener) {
            elems[i].addEventListener(eventName, handler, false);
        }
    }
}
Ñ­»·Ã¿Ö´ÐÐÒ»´Î£¬¶¼»áÅжÏwindow¶ÔÏóµÄattachEvent»òaddEventListenerÊÇ·ñ´æÔÚ£¬ÆäʵÕâ¸ö½öÅжÏÒ»´ÎÒ²¾Í¹»ÁË£»´ËÍ⣬“”on” + eventName”µÄ×Ö·û´®Æ´½ÓÒ²»áÖظ´Ö´ÐС£ÓÅ»¯ÈçÏ£º
function addEvent(elems, eventName, handler) {
    var i = -1, len = elems.length;
    if (window.attachEvent) {
        eventName = “on” + eventName;
        while (++i < len) {
            elems[i].attachEvent(eventName, handler);
        }
    } else if (window.addEventListener) {
        while (++i < len) {
            elems[i].addEventListener(eventName, handler, false);
        }
    }
}

2.2  ¾¡Á¿Ê¹ÓÃÔ­ÉúµÄº¯Êý¶ø²»ÊÇ×Ô¶¨Ò庯Êý

µ±Äã¶ÔJavascriptµÄÄÚÖÃÀàÐͱäÁ¿Ö´ÐÐijÏî²Ù×÷ʱ£¬ÄãÓ¦¸ÃÏȲé²éÕâÏî²Ù×÷ÊÇ·ñÓÐÔ­ÉúµÄ·½·¨¡£
ÒªÉú³ÉÒ»¸öÊý×éµÄ¸±±¾£¬Äã»áÔõô×öÄØ£¿±éÀúÊý×éÔªËØÈ»ºóÖð¸ö¸³Öµµ½ÁíÒ»¸öÊý×飬ÕâËƺõÊÇΨһµÄ·½·¨¡£Æäʵ£¬Ô­ÉúµÄArray.prototype.slice¾Í¿ÉÒÔ´ïµ½¸´ÖƵÄÄ¿µÄ¡£Õâ¸ö·½·¨¿ÉÒÔ´Óij¸öÊý×é·µ»ØÑ¡¶¨µÄÔªËØ£¬ÇÒ²»Ó°ÏìÔ­À´µÄÊý×é¡£Èç¹û²ÎÊýÁô¿Õ£¬·µ»ØµÄ¾ÍÊÇÈ«²¿ÔªËØ¡£
Array.prototype.slice»¹¿ÉÒÔ¶ÔijЩ²»ÊÇÊý×é¶øÓÖÄÜͨ¹ýÊý×ÖË÷Òý·ÃÎʵÄÀàÐͽøÐвÙ×÷£¬±ÈÈçarguments£º
function test() {
    alert(Array.prototype.slice.call(arguments));
}
test(1, 2, 3); // output “1,2,3″
ÔÚFirefoxÏ£¬ËüÉõÖÁ¿ÉÒÔ¶ÔHtmlCollection½øÐвÙ×÷¡£¿ÉϧÔÚIEϲ»ÐС£

ÁíÒ»¸öÀý×ÓÊÇÊý×éÅÅÐò£¬Ò»°ãÇé¿öÏ£¬ÎÒÃDz»ÐèÒªÁíÍâдÅÅÐòËã·¨£¬ÓÃÔ­ÉúµÄArray.prototype.sort¾Í¹»ÁË¡£sort·½·¨Ö»ÓÐÒ»¸ö²ÎÊý£¬¸Ã²ÎÊýÊÇÒ»¸öº¯Êý£¬¾ö¶¨Á½¸öÏà±È½ÏµÄÔªËØË­ÔÚÇ°Ë­ÔÚºó£¬Ä¬ÈÏÊÇ°´ÕÕ×Ö·û˳ÐòÅÅÐò£¬±ÈÈç11»áÅÅÔÚ2֮ǰ¡£Òª°´Êý×Ö´óСÅÅÐò£¬¿ÉÒÔÕâÑùд£º
var arr = [11, 2, 0, 12, 33];
arr.sort(
    function(a, b) {
        return a - b;
    }
);
Ò²¿ÉÒÔ°´ÕÕ¶ÔÏóµÄij¸öÊôÐÔ½øÐÐÅÅÐò£º
var arr = [
    { id : 11 },
    { id : 0 },
    { id : 22 }
];
arr.sort(
    function(a, b) {
        return a.id - b.id;
    }
);

2.3  Êý×éÈ¥Öظ´

ArrayÀàÐͲ¢Ã»ÓÐÌṩȥÖظ´µÄ·½·¨£¬Èç¹ûÒª°ÑÊý×éµÄÖظ´ÔªËظɵô£¬ÄǵÃ×Ô¼ºÏë°ì·¨£º
function unique(arr) {
    var result = [], isRepeated;
    for (var i = 0, len = arr.length; i < len; i++) {
        isRepeated = false;
        for (var j = 0, len = result.length; j < len; j++) {
            if (arr[i] == result[j]) {   
                isRepeated = true;
                break;
            }
        }
        if (!isRepeated) {
            result.push(arr[i]);
        }
    }
    return result;
}
×ÜÌå˼·ÊÇ°ÑÊý×éÔªËØÖð¸ö°áÔ˵½ÁíÒ»¸öÊý×飬°áÔ˵Ĺý³ÌÖмì²éÕâ¸öÔªËØÊÇ·ñÓÐÖظ´£¬Èç¹ûÓоÍÖ±½Ó¶ªµô¡£´ÓǶÌ×Ñ­»·¾Í¿ÉÒÔ¿´³ö£¬ÕâÖÖ·½·¨Ð§Âʼ«µÍ¡£ÎÒÃÇ¿ÉÒÔÓÃÒ»¸öhashtableµÄ½á¹¹¼Ç¼ÒÑÓеÄÔªËØ£¬ÕâÑù¾Í¿ÉÒÔ±ÜÃâÄÚ²ãÑ­»·¡£Ç¡ºÃ£¬ÔÚJavascriptÖÐʵÏÖhashtableÊǼ«Îª¼òµ¥µÄ£¬¸Ä½øÈçÏ£º
function unique(arr) {
    var result = [], hash = {};
    for (var i = 0, elem; (elem = arr[i]) != null; i++) {
        if (!hash[elem]) {
            result.push(elem);
            hash[elem] = true;
        }
    }
    return result;
}

Èý¡¢Ê¹´úÂë¸üÒ׶Á¡¢¸üºÃά»¤

ÎÞÂÛÊÇÔÚ¿ª·¢Öл¹ÊÇ¿ª·¢ºó£¬±£³Ö´úÂëÇåÎúÒ׶Á¿ÉÒÔ¸ü¿ì¸ü׼ȷµØÐ޸ĴúÂë¡£

3.1  Á¬½ÓHTML×Ö·û´®

ÏàÐÅ×öÇ°¶Ë¿ª·¢µÄÅóÓѶ¼ÊܹýÕâ¸öÕÛÄ¥£ºÁ¬½ÓHTMLµÄʱºò±»¿É¶ñµÄµ¥ÒýºÅ¡¢Ë«ÒýºÅ¸ãµÃÍ·»èÄÔÕÍ¡£±ÈÈ磺
element.innerHTML = ‘<a href=”‘ + url + ‘” onclick=”alert(\” + msg + ‘\’);”>’ + text + ‘</a>’;
ÕâÀï½éÉÜÒ»¸ö×Ö·û´®¸ñʽ»¯º¯Êý£º
String.format = function(str) {
    var args = arguments, re = new RegExp(”%([1-" + args.length + "])”, “g”);
    return String(str).replace(
        re,
        function($1, $2) {
            return args[$2];
        }
    );
};
µ÷Ó÷½·¨ºÜ¼òµ¥£º
element.innerHTML = String.format(’<a href=”%1″ onclick=”alert(\’%2\’);”>%3</a>’, url, msg, text);
Òâ˼¾ÍÊÇÓõÚn¸ö²ÎÊý°Ñ%nÌæ»»µô¡£ÕâÑùÒ»À´ÇåÎú¶àÁË°É¡£

3.2  ΪÄúµÄ³ÌÐò´òÔìÒ»¸öConfigÅäÖöÔÏó

±àдJava»òÕßC#³ÌÐòµÄʱºò£¬ÎÒÃÇÒ»°ã»á´ÓXML¶ÁÈ¡³ÌÐòµÄÅäÖÃÐÅÏ¢¡£ÔÚJavascriptÀïÃ棬ÓÃXML×öÅäÖÃÐÅÏ¢²»´ó»®Ë㣬һ·½ÃæÒª¶àÇëÇóÒ»¸öXMLÎļþ»òÕß°ÑXML×Ö·û´®×ª»»ÎªXML¶ÔÏó£¬ÁíÒ»·½ÃæXML¶ÔÏóµÄ·½·¨±È½Ï¸´ÔÓÈß³¤¡£ÇáÁ¿¼¶µÄJSONÊÇ×îºÃµÄÑ¡Ôñ¡£
³ÌÐòÖеij£Á¿Ó¦¸Ã·Åµ½ConfigÅäÖöÔÏóÖУ¬±ÈÈçAjaxÇëÇóµÄUrl¡¢Ä³¸ö²Ù×÷µÄÌáʾµÈ£¬ÀýÈ磺
var Config = {
    ajaxUrl : “test.jsp”,
    successTips : “ÇëÇóÍê³É”
};
Èç¹ûConfigµÄÊýÁ¿½Ï¶à£¬¿ÉÒÔ¸ù¾ÝÅäÖÃÀàÐͶàǶÌ×Ò»²ã£¬±ÈÈ磺
var Config = {
    url : {
        src1 : “test1.jsp”,
        src2 : “test2.jsp”,
        .
        .
    },
    tips : {
        src1Suc : “ÇëÇó1Íê³É”,
        src2Suc: “ÇëÇó2Íê³É”,
        .
        .
    }
};
ConfigÓ¦·ÅÖÃÓÚ³ÌÐòµÄ×îÇ°Ã棬·½±ã²é¿´ºÍÐ޸ġ£

(ÔðÈα༭£ºadmin)
Ö¯ÃζþάÂëÉú³ÉÆ÷
¶¥Ò»ÏÂ
(0)
0%
²ÈÒ»ÏÂ
(0)
0%
------·Ö¸ôÏß----------------------------
·¢±íÆÀÂÛ
Çë×Ô¾õ×ñÊØ»¥ÁªÍøÏà¹ØµÄÕþ²ß·¨¹æ£¬ÑϽû·¢²¼É«Çé¡¢±©Á¦¡¢·´¶¯µÄÑÔÂÛ¡£
ÆÀ¼Û:
񡀂:
Óû§Ãû: ÑéÖ¤Âë:µã»÷ÎÒ¸ü»»Í¼Æ¬