2013-11-25 29 views
3

我正在使用select2。內置事件不支持選擇項目上的懸停事件,因此我試圖在鼠標懸停在項目上(多重選擇)時觸發懸停事件。然而如何在select2選項上啓用懸停事件

function tagFormat(state) { 
    return '<span class="tagElement">' + state.text + '</span>'; 
} 
$('.tagElement').hover(function(event){ 
    alert('something'); 
}); 
var sampleTags = ['red','white']; 
$("[taggable]").select2({ 
    formatSelection: tagFormat, 
    maximumInputLength: 10, 
    tags:sampleTags, 
    tokenSeparators: [",", " "] 
}); 
http://bootply.com/96527

Bootply,你可以看到外面的選擇2懸停事件做內選擇2火事實並非如此。裏面的機制是什麼?

回答

1

select2.js具有的功能,.show,其更新屬性「詠歎調 - describedby」當你將鼠標懸停在一個選項。此功能可以擴展...

jsfiddle example

c.prototype.show = function() { 
    var b = a.Event("show.bs." + this.type); 
    this.$element.trigger(b); 
    var d = a.contains(this.$element[0].ownerDocument.documentElement, this.$element[0]); 
    if (b.isDefaultPrevented() || !d) return; 
    var e = this, 
     f = this.tip(), 
     g = this.getUID(this.type); 
    /*look for the select box and find the 'onmouseover' attribute */ 
    var s = this.$element.siblings('select'); 

    if (s != undefined && s.attr('onmouseover') != undefined) { 
     var m = s.attr('onmouseover'); 
     eval(m); 
    } 
    ... 
}