詳解jquery事件delegate()的使用方法,jquerydelegate

來源:互聯網
上載者:User

詳解jquery事件delegate()的使用方法,jquerydelegate

我們先看官方是怎麼說delegate()方法,delegate() 方法為指定的元素(屬於被選元素的子項目)添加一個或多個事件處理常式,並規定當這些事件發生時啟動並執行函數,使用 delegate() 方法的事件處理常式適用於當前或未來的元素(比如由指令碼建立的新元素)。
文法很簡單

$(selector).delegate(childSelector,event,data,function)

參數說明

  • childSelector 必需。規定要附加事件處理常式的一個或多個子項目。
  • event 必需。規定附加到元素的一個或多個事件。
  • 由空格分隔多個事件值。必須是有效事件。
  • data 可選。規定傳遞到函數的額外資料。
  • function 必需。規定當事件發生時啟動並執行函數。

delegate 的函數是被某一類型的共同父元素調用。

listNode.delegate('.condition-remove','click',function(e){e.preventDefault();$(this).parents('.search-condition-item').remove();});

完整執行個體(要實現的效果)

function renderSearchConditions(selectionId,conditions){var conditionsTemplate = '<div class="search-conditions-list-section">'+'<ul class="search-conditions-list"></ul>'+'</div>',listNode = $(conditionsTemplate);listItemTemplate = '<li class="search-condition-item" data-type="{conditonType}"><span>{condition}</span><a class="condition-remove" href="#">x</a></li>';for(var key in conditions){var condition = conditions[key].keyword,conditionType = conditions[key].type,listItemNode = $.substitute(listItemTemplate,{conditionType:conditionType,condition:condition});listNode.append(listItemNode);}$(selectionId).prepend(listNode);listNode.delegate('.condition-remove','click',function(e){e.preventDefault();$(this).parents('.search-condition-item').remove();});}

1.綁定的事務中,擷取事務源,調用hide辦法,並傳入事務來源物件:

$(document).delegate("body", "click", function(e) {  var ev = e || window.event; // 事務  //var target = ev.target || ev.srcElement; // 獲得事務源  hide(ev.target || ev.srcElement, true); });

底本用的$(window),然則IE8之前的,貌似有bug。
$(document)不好之處,頁面載入完後,會觸發一次...
2. hide辦法中,斷定該事務源是否從指定元素中發出,即事務源元素是不是指定元素的子項目或者其本身。

//子項目斷定==== if (!!window.find)HTMLElement.prototype.contains = function(B) {  return this.compareDocumentPosition(B) - 19 > 0 }; function hide(dom, isClick) {  var nn,t,_isClick = !!isClick;  try {   for (var n in objList) {    nn = objList[n];    t = nn.getOption("target")[0];    if (_isClick && (t == dom || t.contains(dom)))return;    if (!_isClick || !nn.box[0].contains(dom)) nn.hide();   }  } catch(e) {  } }

3.上方的hide辦法中,isClick變數 斷定 是不是點擊事務觸發的。以便處理懲罰resize的。resize用settimeout作處理懲罰,降落記憶體消費。

var reTime = null; $(window).bind("resize", function() {  if (reTime) clearTimeout(reTime);  reTime = setTimeout(hide, 50); });

以上就是關於jquery事件delegate()的使用方法相關內容,希望對大家的學習有所協助。

您可能感興趣的文章:
  • jQuery中的.bind()、.live()和.delegate()之間區別分析
  • jQuery事件 delegate()使用方法介紹
  • 由點擊頁面其它地方隱藏div所想到的jQuery的delegate
  • jQuery中delegate與on的用法與區別樣本介紹
  • jQuery中bind,live,delegate與one方法的用法及區別解析
  • jQuery:delegate中select()不起作用的解決方案(執行個體講解)
  • jQuery中delegate和on的用法與區別詳細解析
  • jQuery事件綁定on()、bind()與delegate() 方法詳解
  • 詳解jquery事件delegate()的使用方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.