jQuery¼¼Êõ

jQuery¼¼ÇÉÖ®ÈÃÈκÎ×é¼þ¶¼Ö§³ÖÀàËÆDOMµÄʼþ¹ÜÀí

×ÖºÅ+ ×÷ÕߣºH5Ö®¼Ò À´Ô´£ºH5Ö®¼Ò 2016-04-16 10:05 ÎÒÒªÆÀÂÛ( )

ÕâƪÎÄÕÂÖ÷Òª½éÉÜÁËjQuery¼¼ÇÉÖ®ÈÃÈκÎ×é¼þ¶¼Ö§³ÖÀàËÆDOMµÄʼþ¹ÜÀí µÄÏà¹Ø×ÊÁÏ,ÐèÒªµÄÅóÓÑ¿ÉÒԲο¼Ï ±¾ÎĽéÉÜÒ»¸öjqueryµÄС¼¼ÇÉ£¬ÄÜÈÃÈÎÒâ×é¼þ¶ÔÏó¶¼ÄÜÖ§³ÖÀà

±¾ÎĽéÉÜÒ»¸öjqueryµÄС¼¼ÇÉ£¬ÄÜÈÃÈÎÒâ×é¼þ¶ÔÏó¶¼ÄÜÖ§³ÖÀàËÆDOMµÄʼþ¹ÜÀí£¬Ò²¾ÍÊÇ˵³ýÁËÅÉ·¢Ê¼þ£¬Ìí¼Ó»òɾ³ýʼþ¼àÌýÆ÷£¬»¹ÄÜÖ§³ÖʼþðÅÝ£¬×èֹʼþĬÈÏÐÐΪµÈµÈ¡£ÔÚjqueryµÄ°ïÖúÏ£¬Ê¹ÓÃÕâ¸ö·½·¨À´¹ÜÀíÆÕͨ¶ÔÏóµÄʼþ¾Í¸ú¹ÜÀíDOM¶ÔÏóµÄʼþһģһÑù£¬ËäÈ»ÔÚ×îºóµ±Äã¿´µ½Õâ¸öС¼¼ÇɵľßÌåÄÚÈÝʱ£¬Äã¿ÉÄÜ»á¾õµÃÔ­À´Èç´Ë»òÕß²»¹ýÈç´Ë£¬µ«ÊÇÎÒ¾õµÃÈç¹ûÄÜ°ÑÆÕͨµÄ·¢²¼-¶©ÔÄģʽµÄʵÏָijÉDOMÀàËƵÄʼþ»úÖÆ£¬ÄÇ¿ª·¢³öÀ´µÄ×é¼þÒ»¶¨»áÓиü´óµÄÁé»îÐÔºÍÀ©Õ¹ÐÔ£¬¶øÇÒÎÒÒ²ÊǵÚÒ»´ÎʹÓÃÕâÖÖ·½·¨£¨¼ûʶ̫dzµÄÔ­Òò£©£¬¾õµÃËüµÄʹÓüÛÖµ»¹Âù´óµÄ£¬ËùÒԾͰÑËü·ÖÏí³öÀ´ÁË¡£

ÔÚÕýʽ½éÉÜÕâ¸ö¼¼ÇÉ֮ǰ£¬µÃÏÈ˵һÏÂÎÒ֮ǰ¿¼ÂǵÄÒ»ÖÖ·½·¨£¬Ò²¾ÍÊÇ·¢²¼-¶©ÔÄģʽ£¬¿´¿´ËüÄܽâ¾öʲôÎÊÌâÒÔ¼°Ëü´æÔÚµÄÎÊÌâ¡£

1. ·¢²¼-¶©ÔÄģʽ

ºÜ¶à²©¿Í°üÀ¨Êé±¾É϶¼ËµjavascriptҪʵÏÖ×é¼þµÄ×Ô¶¨ÒåʼþµÄ»°£¬¿ÉÒÔ²ÉÓ÷¢²¼-¶©ÔÄģʽ£¬Æð³õÎÒÒ²ÊǼᶨ²»ÒƵØÕâôÈÏΪµÄ£¬ÓÚÊÇÓÃjqueryµÄ$.CallbacksдÁËÒ»¸ö£º

define(function(require, exports, module) { var $ = require('jquery'); var Class = require('./class'); function isFunc(f) { return Object.prototype.toString.apply(f) === '[object Function]'; } /** * Õâ¸ö»ùÀà¿ÉÒÔÈÃÆÕͨµÄÀà¾ß±¸Ê¼þÇý¶¯µÄÄÜÁ¦ * ÌṩÀàËÆjqµÄon off trigger·½·¨£¬²»¿¼ÂÇone·½·¨£¬Ò²²»¿¼ÂÇÃüÃû¿Õ¼ä * ¾ÙÀý£º * var e = new EventBase(); * e.on('load', function(){ * console.log('loaded'); * }); * e.trigger('load');//loaded * e.off('load'); */ var EventBase = Class({ instanceMembers: { init: function () { this.events = {}; //°Ñ$.CallbacksµÄflagÉèÖóÉÒ»¸öʵÀýÊôÐÔ£¬ÒÔ±ã×ÓÀà¿ÉÒÔ¸²¸Ç this.CALLBACKS_FLAG = 'unique'; }, on: function (type, callback) { type = $.trim(type); //Èç¹ûtype»òÕßcallback²ÎÊýÎÞЧÔò²»´¦Àí if (!(type && isFunc(callback))) return; var event = this.events[type]; if (!event) { //¶¨ÒåÒ»¸öеÄjq¶ÓÁУ¬ÇҸöÓÁв»ÄÜÌí¼ÓÖظ´µÄ»Øµ÷ event = this.events[type] = $.Callbacks(this.CALLBACKS_FLAG); } //°ÑcallbackÌí¼Óµ½Õâ¸ö¶ÓÁÐÖУ¬Õâ¸ö¶ÓÁпÉÒÔͨ¹ýtypeÀ´·ÃÎÊ event.add(callback); }, off: function (type, callback) { type = $.trim(type); if (!type) return; var event = this.events[type]; if (!event) return; if (isFunc(callback)) { //Èç¹ûͬʱ´«µÝtype¸úcallback£¬Ôò½«callback´Ótype¶ÔÓ¦µÄ¶ÓÁÐÖÐÒƳý event.remove(callback); } else { //·ñÔò¾ÍÒƳýÕû¸ötype¶ÔÓ¦µÄ¶ÓÁÐ delete this.events[type]; } }, trigger: function () { var args = [].slice.apply(arguments), type = args[0];//µÚÒ»¸ö²ÎÊýתΪtype type = $.trim(type); if (!type) return; var event = this.events[type]; if (!event) return; //ÓÃʣϵIJÎÊýÀ´´¥·¢type¶ÔÓ¦µÄ»Øµ÷ //ͬʱ°Ñ»Øµ÷µÄÉÏÏÂÎÄÉèÖóɵ±Ç°ÊµÀý event.fireWith(this, args.slice(1)); } } }); return EventBase; });

£¨»ùÓÚseajsÒÔ¼°¡¶Ïê½âJavascriptµÄ¼Ì³ÐʵÏÖ¡·½éÉܵļ̳пâclass.js£©

Ö»ÒªÈκÎ×é¼þ¼Ì³ÐÕâ¸öEventBase£¬¾ÍÄܼ̳ÐËüÌṩµÄon off trigger·½·¨À´Íê³ÉÏûÏ¢µÄ¶©ÔÄ£¬·¢²¼ºÍÈ¡Ïû¶©ÔŦÄÜ£¬±ÈÈçÎÒÏÂÃæÏëҪʵÏÖµÄÕâ¸öFileUploadBaseView£º

define(function(require, exports, module) { var $ = require('jquery'); var Class = require('./class'); var EventBase = require('./eventBase'); var DEFAULTS = { data: [], //ҪչʾµÄÊý¾ÝÁÐ±í£¬ÁбíÔªËرØÐëÊÇobjectÀàÐ͵ģ¬Èç[{url: 'xxx.png'},{url: 'yyyy.png'}] sizeLimit: 0, //ÓÃÀ´ÏÞÖÆBaseViewÖеÄչʾµÄÔªËظöÊý£¬Îª0±íʾ²»ÏÞÖÆ readonly: false, //ÓÃÀ´¿ØÖÆBaseViewÖеÄÔªËØÊÇ·ñÔÊÐíÔö¼ÓºÍɾ³ý onBeforeRender: $.noop, //¶ÔÓ¦beforeRenderʼþ£¬ÔÚrender·½·¨µ÷ÓÃÇ°´¥·¢ onRender: $.noop, //¶ÔÓ¦renderʼþ£¬ÔÚrender·½·¨µ÷Óú󴥷¢ onBeforeAppend: $.noop, //¶ÔÓ¦beforeAppendʼþ£¬ÔÚappend·½·¨µ÷ÓÃÇ°´¥·¢ onAppend: $.noop, //¶ÔÓ¦appendʼþ£¬ÔÚappend·½·¨µ÷Óú󴥷¢ onBeforeRemove: $.noop, //¶ÔÓ¦beforeRemoveʼþ£¬ÔÚremove·½·¨µ÷ÓÃÇ°´¥·¢ onRemove: $.noop //¶ÔÓ¦removeʼþ£¬ÔÚremove·½·¨µ÷Óú󴥷¢ }; /** * Êý¾Ý½âÎö£¬¸øÿ¸öÔªËصÄÌí¼ÓÒ»¸öΨһ±êʶ_uuid£¬·½±ã²éÕÒ */ function resolveData(ctx, data){ var time = new Date().getTime(); return $.map(data, function(d){ d._uuid = '_uuid' + time + Math.floor(Math.random() * 100000); }); } var FileUploadBaseView = Class({ instanceMembers: { init: function (options) { this.base(); this.options = this.getOptions(options); }, getOptions: function(options) { return $.extend({}, DEFAULTS, options); }, render: function(){ }, append: function(data){ }, remove: function(prop){ } }, extend: EventBase }); return FileUploadBaseView; });

ʵ¼Êµ÷ÓòâÊÔÈçÏ£º

ʼþ¹ÜÀí,DOM,jQuery¼¼ÇÉ


²âÊÔÖУ¬ÊµÀý»¯ÁËÒ»¸öFileUploadBaseView¶ÔÏóf£¬²¢ÉèÖÃÁËËüµÄnameÊôÐÔ£¬Í¨¹ýon·½·¨Ìí¼ÓÒ»¸ö¸úhelloÏà¹ØµÄ¼àÌýÆ÷£¬×îºóͨ¹ýtrigger·½·¨´¥·¢ÁËhelloµÄ¼àÌýÆ÷£¬²¢´«µÝÁ˶îÍâµÄÁ½¸ö²ÎÊý£¬ÔÚ¼àÌýÆ÷ÄÚ²¿³ýÁË¿ÉÒÔͨ¹ý¼àÌýÆ÷µÄº¯Êý²ÎÊý·ÃÎʵ½trigger´«µÝ¹ýÀ´µÄÊý¾Ý£¬»¹ÄÜͨ¹ýthis·ÃÎÊf¶ÔÏó¡£

´ÓÄ¿Ç°µÄ½á¹ûÀ´Ëµ£¬Õâ¸ö·½Ê½¿´ÆðÀ´»¹²»´í£¬µ«ÊÇÔÚÎÒÏëÒª¼ÌÐøʵÏÖFileUploadBaseViewµÄʱºòÅöµ½ÁËÎÊÌâ¡£Äã¿´ÎÒÔÚÉè¼ÆÕâ¸ö×é¼þµÄʱºòÄǼ¸¸ö¶©ÔÄÏà¹ØµÄoption£º 

ʼþ¹ÜÀí,DOM,jQuery¼¼ÇÉ

¡¡

1.±¾Õ¾×ñÑ­ÐÐÒµ¹æ·¶£¬ÈκÎתÔصĸå¼þ¶¼»áÃ÷È·±ê×¢×÷ÕߺÍÀ´Ô´£»2.±¾Õ¾µÄÔ­´´ÎÄÕ£¬ÇëתÔØʱÎñ±Ø×¢Ã÷ÎÄÕÂ×÷ÕߺÍÀ´Ô´£¬²»×ðÖØÔ­´´µÄÐÐΪÎÒÃǽ«×·¾¿ÔðÈΣ»3.×÷ÕßͶ¸å¿ÉÄܻᾭÎÒÃDZ༭Ð޸Ļò²¹³ä¡£

Ïà¹ØÎÄÕÂ
  • jQuery¸ß¼¶¼¼ÇÉ¡ª¡ªDOM²Ù×÷ƪ£¬jquery¼¼ÇÉdom

    jQuery¸ß¼¶¼¼ÇÉ¡ª¡ªDOM²Ù×÷ƪ£¬jquery¼¼ÇÉdom

    2016-04-16 11:00

  • jquery¼¼ÇÉ×ܽá

    jquery¼¼ÇÉ×ܽá

    2016-03-20 18:00

  • jQuery¼¼ÇÉ»ã×Ü

    jQuery¼¼ÇÉ»ã×Ü

    2016-03-20 17:02

  • jquery¶ÔÏóºÍJavaScript¶ÔÏó¼´DOM¶ÔÏóÏ໥ת»»

    jquery¶ÔÏóºÍJavaScript¶ÔÏó¼´DOM¶ÔÏóÏ໥ת»»

    2016-03-06 17:00

ÍøÓѵãÆÀ
Ô