我使用的引導,我看着它的jQuery插件bootstrap-alert.js
,我不能完全得到後援代碼下面貼:爲我解釋bootstrap-alert.js?
!function($){
"use strict"
/* ALERT CLASS DEFINITION
* ====================== */
var dismiss = '[data-dismiss="alert"]'
, Alert = function (el) {
$(el).on('click', dismiss, this.close)
}
Alert.prototype = {
constructor: Alert
, close: function (e) {
var $this = $(this)
, selector = $this.attr('data-target')
, $parent
if (!selector) {
selector = $this.attr('href')
selector = selector && selector.replace(/.*(?=#[^\s]*$)/, '') //strip for ie7
}
$parent = $(selector)
$parent.trigger('close')
e && e.preventDefault()
$parent.length || ($parent = $this.hasClass('alert') ? $this : $this.parent())
$parent.removeClass('in')
function removeElement() {
$parent.remove()
$parent.trigger('closed')
}
$.support.transition && $parent.hasClass('fade') ?
$parent.on($.support.transition.end, removeElement) :
removeElement()
}
}
/* ALERT PLUGIN DEFINITION
* ======================= */
$.fn.alert = function (option) {
return this.each(function() {
var $this = $(this)
, data = $this.data('alert')
if (!data) $this.data('alert', (data = new Alert(this)))
if (typeof option == 'string') data[option].call($this)
})
}
$.fn.alert.Constructor = Alert
/* ALERT DATA-API
* ============== */
$(function() {
$('body').on('click.alert.data-api', dismiss, Alert.prototype.close)
})
}(window.jQuery)
這是幾乎所有的插件中最簡單的一個given.What我不明白的是
1.what的selector
?data-target
是無處找到...那麼,什麼$this.attr('data-target')
做什麼?
2.這個插件如何作爲一個整體?正如文檔所說,它有三個部分,它們是如何相互作用的?
編輯:還有一些我不太understand.This部分:
functionremoveElement() { $parent.remove() $parent.trigger('closed') }
首先你刪除的元素,然後以某種方式調用它完全不使用定義的函數一些物體已被移除。closed
在哪裏? 謝謝,G
謝謝〜你能解釋一下'如果選擇器= $ this.attr('href') selector = selector && selector.replace(/.*(?=#[^ \ s] * $)/,'')// strip for ie7 }部分呢?對我來說,它就像'a =「#」',所以選擇器結束了'#'。並且'$ parent.length || ($ parent = $ this.hasClass('alert')?$ this:$ this.parent())'part.it可以寫在'if'子句中,對嗎?這使用'||'? – Gnijuohz 2012-03-14 03:22:58
查看修改...... – RoToRa 2012-03-19 12:39:59