[ 2007-8-6 17:03:00 | By: ibrahim ]
javascript²Ù×÷Select±ê¼ÇÖÐoptions¼¯ºÏ
ÏÈÀ´¿´¿´options¼¯ºÏµÄÕ⼸¸ö·½·¨£º
options.add(option)·½·¨Ïò¼¯ºÏÀïÌí¼ÓÒ»Ïîoption¶ÔÏó£»
options.remove(index)·½·¨ÒƳýoptions¼¯ºÏÖеÄÖ¸¶¨Ï
options(index)»òoptions.item(index)¿ÉÒÔͨ¹ýË÷Òý»ñÈ¡options¼¯ºÏµÄÖ¸¶¨Ï
javascript´úÂëÈçÏ£º
var selectTag = null; //select񈬀
var OPTONLENGTH = 10; //ÿ´ÎÌî³äoptionÊý
var colls = []; //¶Ôselect±ê¼ÇoptionsµÄÒýÓÃ
window.onload = function(){
selectTag = document.getElementById("SelectBox"); //»ñÈ¡select±ê¼Ç
colls = selectTag.options; //»ñÈ¡ÒýÓÃ
//init ......
Èç¹ûÄãÏë½øÒ»²½Á˽âÈçºÎÓÃJavaScriptÀ´ÎªÍøÒ³Ìí¼Ó½»»¥ÐԵĻ°£¬ÄãÒ²ÐíÒѾÌý¹ýJavaScriptµÄʼþ´úÀí£¨event delegation£©ÁË£¬²¢ÇÒ»á¾õµÃÖ»ÓÐÄÇЩţ±ÆºæºæµÄJavaScript³ÌÐòÔ±²Å»á¹ØÐÄÕâÑù¸´ÔÓµÄÉè¼Æģʽ¡£ÊÂʵÉÏ£¬Èç¹ûÄãÒѾ֪µÀÔõôÑùÈ¥Ìí¼ÓJavaScriptµÄʼþ´¦ÀíÆ÷£¨event handler£©£¬ÊµÏÖʼþ´úÀíÒ²ÊǼþÇá¶øÒ×¾ÙµÄÊÂÇé¡£
JavaScriptʼþÊÇËùÓÐÍøÒ³½»»¥ÐԵĸù»ù£¨ÎÒÖ¸µÄÊÇÕæÕýµÄ»¥¶¯ÐÔ£¬¶ø²»½öÊÇÄÇЩCSSµÄÏÂÀ²Ëµ¥£©¡£ÔÚ´«Í³µÄʼþ´¦ÀíÖУ¬Äã°´ÕÕÐèҪΪÿһ¸öÔªËØÌí¼Ó»òÕßÊÇɾ³ýʼþ´¦ÀíÆ÷¡£È»¶ø£¬Ê¼þ´¦ÀíÆ÷½«ÓпÉÄܵ¼ÖÂÄÚ´æ鶻òÕßÊÇÐÔÄÜϽµ——ÄãÓõÃÔ½¶àÕâÖÖ·çÏÕ¾ÍÔ½´ó¡£JavaScriptʼþ´úÀíÔòÊÇÒ»ÖÖ¼òµ¥µÄ¼¼ÇÉ£¬Í¨¹ýËüÄã¿ÉÒÔ°Ñʼþ´¦ÀíÆ÷Ìí¼Óµ½Ò»¸ö¸¸¼¶ÔªËØÉÏ£¬ÕâÑù¾Í±ÜÃâÁË°Ñʼþ´¦ÀíÆ÷Ìí¼Óµ½¶à¸ö×Ó¼¶ÔªËØÉÏ¡£
ËüÊÇÔõôÔË×÷µÄÄØ£¿
ʼþ´úÀíÓõ½ÁËÁ½¸öÔÚJavaSciprtʼþÖг£±»ºöÂÔµÄÌØÐÔ£ºÊ¼þðÅÝÒÔ¼°Ä¿±êÔªËØ¡£µ±Ò»¸öÔªËØÉϵÄʼþ±»´¥·¢µÄʱºò£¬±ÈÈç˵Êó±êµã»÷ÁËÒ»¸ö°´Å¥£¬Í¬ÑùµÄʼþ½«»áÔÚÄǸöÔªËصÄËùÓÐ×æÏÈÔªËØÖб»´¥·¢¡£ÕâÒ»¹ý³Ì±»³ÆΪʼþðÅÝ£»Õâ¸öʼþ´ÓÔʼԪËØ¿ªÊ¼Ò»Ö±Ã°Åݵ½DOMÊ÷µÄ×îÉϲ㡣¶ÔÈκÎÒ»¸öʼþÀ´Ëµ£¬ÆäÄ¿±êÔªËض¼ÊÇÔʼԪËØ£¬ÔÚÎÒÃǵ ......
indexOf
·µ»ØÔªËØÔÚÊý×éµÄË÷Òý£¬Ã»ÓÐÔò·µ»Ø-1¡£ÓëstringµÄindexOf·½·¨²î²»¶à¡£
Èç¹ûÆäËûä¯ÀÀÆ÷ûÓÐʵÏÖ´Ë·½·¨£¬¿ÉÒÔÓÃÒÔÏ´úÂëʵÏÖ¼æÈÝ£º
Array.prototype.indexOf = function(el, start) {
var start = start || 0;
for ( var i=0; i < this.length; ++i ) {
if ( this[i] === el ) {
return i;
}
}
return -1;
};
var array = [2, 5, 9];
var index = array.indexOf(2);
// index is 0
index = array.indexOf(7);
// index is -1
lastIndexOf
ÓëstringµÄlastIndexOf·½·¨²î²»¶à¡£
Èç¹ûÆäËûä¯ÀÀÆ÷ûÓÐʵÏÖ´Ë·½·¨£¬¿ÉÒÔÓÃÒÔÏ´úÂëʵÏÖ¼æÈÝ£º
Array.prototype.indexOf = function(el, start) {
var start = start || 0;
for ( var i=0; i < this.length; ++i ) {
if ( this[i] === el ) {
return i;
}
}
return -1;
};
forEach
¸÷Àà¿âÖж¼ÊµÏÖÏàËƵÄeach·½·¨¡£
Èç¹ûÆäËûä¯ÀÀÆ÷ûÓÐʵÏÖ´Ë·½·¨£¬¿ÉÒÔÓÃÒÔÏ´úÂëʵÏÖ¼æÈÝ£º
Array.prototype.forEach = function(fn, thisObj) {
var scope = ......
Ò»¡¢ÉùÃ÷×Ö·û´®:
var normal_monkey = "I am a monkey!<br>";
document.writeln("Normal monkey " + normal_monkey);
var bold_monkey = normal_monkey.bold();
document.writeln("Bold monkey " + bold_monkey);
ÕâÀïµÄÉùÃ÷£º var bold_monkey = normal_monkey.bold();
ºÍÏÂÃæ¶ÔÉùÃ÷ÊǵÈͬµÄ£º
var bold_monkey = "<b>" + normal_monkey + "</b>";
µÚ1¸ö°æ±¾µÄÉùÃ÷¿´ÆðÀ´Òª¼òÃ÷µÃ¶à¡£ÕâÀïÓõ½ÁË×Ö·û´®¶ÔÏóÖÐ
µÄbold¶ÔÏó£¬ÆäËûµÄ×Ö·û´®¶ÔÏó»¹ÓÐindexOf, charAt,
substring, ÒÔ¼°split, ÕâЩ·½·¨¿ÉÒÔÉîÈë×Ö·û´®µÄ×é³É½á¹¹¡£
Ê×ÏÈÎÒÃÇÑо¿Ò»ÏÂindexOf¡£
2¡¢indexOf
indexOfÓÃÓÚ·¢ÏÖһϵÁеÄ×Ö·ûÔÚÒ»¸ö×Ö·û´®ÖеÈλÖò¢¸æËßÄã×Ó×Ö·û´®µÄÆðʼλÖá£Èç
¹ûÒ»¸ö×Ö·û´®Öв¿°üº¬¸Ã×Ó×Ö·û´®ÔòindexOf·µ»Øreturns "-1."
Àý×Ó£º
var the_word = "monkey";
//ÈÃ ......
ÔÚJavaÖУ¬»ù±¾ÀàÐÍÖ®¼äµÄÇ¿ÖÆת»»Ò²²»ÊÇÕâÑùµÄ£¬±ÈÈ磬ÕûÊýҪת»»³É×Ö·û´®£¬±ØÐëʹÓÃInteger.toString()¾²Ì¬·½·¨»òÕßString.valueOf()¾²Ì¬·½·¨£¬°Ñ×Ö·û´®×ª»»ÎªÕûÊý£¬±ØÐëʹÓÃInteger.valueOf()¡£
¿É¼û£¬²»ÄÜ°ÑJavaScriptÖеÄÀàÐÍת»»¿´×÷Ϊ“Ç¿ÖÆÀàÐÍת»»”¡£
ÔÚJavaScriptÖУ¬DoubleÀàÐͺÍIntÀàÐͶ¼ÊÇ¿´×÷ΪNumber¶ÔÏó£¬Òò´ËÎÞÂÛÊÇtypeof 1»¹ÊÇtypeof 1.0£¬¶¼ÊÇ·µ»Ønumber¡£ÕâÑùÎÒÃÇ¿ÉÒÔ²»ÓÃÈ¥¹ÜÊÇInt»¹ÊÇDoubleÀàÐÍ£¬ÈÃJavaScript½âÊÍÒýÇæÄÚ²¿È¥´¦Àí¡£
Èç¹ûÒª°ÑNumberת»»³ÉString£¬¿ÉÒÔʹÓÃNumberµÄtoString()·½·¨£¬£¨Ïó(1).toString()À¨ºÅ±ØÐë»òÕß 1 .toString()¿Õ¸ñ±ØÐ룬·ñÔò»á±àÒë³ö´í£¬Èç¹ûÊDZäÁ¿ÔòÎÞÐ裩£¬»òÕßµ÷ÓÃString()º¯Êý£¬ÕâÁ½ÖÖ·½·¨¶¼»á×Ô¶¯µ÷ÓýâÊÍÒýÇæÄÚ²¿µÄ NumberToString()£¬»òÕ߸ù¾Ý½øÖƵ÷ÓÃÆäËüº¯Êý£¬»ù±¾ÀàËÆ¡£
Èç¹ûÒª°ÑStringת»»³ÉNumber£¬¿ÉÒÔʹÓÃNumber()º¯Êý£¬Ëû»á×Ô¶¯ÅжÏStringÖÐÊÇÕûÊý»¹ÊǸ¡µãÊý£¬È»ºóÄÚ²¿Ê¹ÓÃÏàÓ¦µÄÊý¾ÝÀàÐÍ£¬ÁíÍâ¿ÉÒÔʹÓÃÈ«¾Öº¯ÊýparseInt()ºÍparseFloat()£¬ËûÃǸù¾ÝÄãµÄÒªÇó½øÐÐת»»¡£Í¬ÑùµÄ£¬ËûÃÇÔÚ½âÊÍÒýÇæÄÚ²¿»úÖÆÉÏÊÇʹÓÃÁË StringToNumber£¬StringToIntµÈµÈÄÚ²¿µÄº¯Êý¡£
¶øÈç¹ûÊÇDoubleת»»³ÉIn ......
1.²»¹ýÎÒ½ÌÄãÒ»¸öѧ×öÍøÒ³µÄ¼¼ÇÉ
ÿ¿´µ½Ò»¸öƯÁÁµÄÍøÒ³¾Í²é¿´Ï´úÂë
2.¿´ÕÅТÏéµÄÊÓƵ
3¡£´Ó»ù´¡Ñ§Æð¡£È»ºóѧЩ»ù±¾µÄWEB¿ª·¢Ó¦ÓÃ
ÍƼöÒ»¸öÍøÕ¾http://www.ccscript.cn/
¸ÃÕ¾Ïêϸ·ÖÀàJSµÄ»ù´¡,¶ÔÏó,¼«ÆäÓ¦Óã»
¸ÃÕ¾ÓжàÖÖµ±½ñÁ÷ÐеÄJS¿ò¼Ü£»
Õ¾ÄÚÖ÷´òJquery¿ò¼ÜµÄ»ù´¡¼°Ó¦Óã»
°üÀ¨ÌØЧ´úÂëºÍչʾÌØЧ£»
Õ¾ÄÚͬʱÌṩ¶àÖÖÆäËüWEB¿ª·¢·þÎñÓ¦Óã»
JS+JQuery QQȺ£º12845737
4.ÍƼöµÄÄÇÈý±¾Ê飨dom±à³ÌÒÕÊõ /javascript ¸ß¼¶³ÌÐòÉè¼Æ /javascript ȨÍþÖ¸ÄÏ£¨Ï¬Å£Ê飩£©¶¼Ï൱²»´í. ²»¹ý³õѧÕß,½¨ÒéÏÈ´Ó<<javascript ȨÍþÖ¸ÄÏ>>Õâ¸ö¿´Æð,Õâ±¾½«µÄ¶«Î÷±È½Ï»ù´¡
5.ÎÒµÄÃؾ÷Ö»ÓÐÈýÌõ£º
£¨1£©Ñ§Ï°ÕÆÎÕJavaScriptµÄ»ù±¾Óï·¨¡£Õâ¸öÈÝÒ×°ìµ½£¬ÕâÀàÊéÌ«¶àÁË£¬½¨ÒéÄã²»Òª¶ÁÌ«ºñµÄÊ飬Ҳ²»Òª¿´Ã»ÓÐÀý×ÓµÄÊé¡£ÕÕÀý×ÓÇã¬Ö´ÐÐһϿ´Ò»ÏÂЧ¹û£¬·ÖÎöÆä¹Ø¼üµãËùÔÚ¡£¿´Àý×Ó²»»áÈÃÄã¸Ð¾õÎÞÁÄ£¡»òÕßÄã¸É´â¾Í¿´Ò»Ï¡¶JavaScript ÓïÑԲο¼¡·ÖÐÎİ棬ÍøÉϵ½´¦¿ÉÒÔÏÂÔص½¡£ÓÈÆäÊÇÔÐ͵ĸÅÄºÜ¹Ø¼üÓ´^_^£¬
£¨2£©ÀûÓóÉÊìµÄ¿ªÔ´£¬Ñ§Ï°Æ亯Êý¿âµÄʹÓ÷½·¨¡£ÕâµãºÜÖØÒª£¬ÔÚÒÔºóµÄ¹¤×÷ÖУ¬Ðí¶à¶«Î÷²»ÐèÒª×Ô¼º´ÓµÍ²ãдµÄ£¬ÓÈÆäÊÇJavaScriptÕâÖÖ²»Ì«ºÃµ÷ÊÔ£¬Ò²²»Ì«ºÃ ......