2012-04-26 59 views
2

Hy,我有一個女巫的形式,我有一些選擇框,我用一個小而簡單的小jquery插件來幫助我設計它們。Jquery問題的方法

$.fn.extend({ 
customSelect : function(options) { 

    if(!$.browser.msie || ($.browser.msie&&$.browser.version>6)){ 
    return this.each(function() { 

     var outerClass = options.customClass, 
      innerClass = options.customClass_inner; 

     var currentSelected = $(this).find(':selected'); 
     var html = currentSelected.html(); 
     if(!html){ html=' '; } 
     $(this).after('<span class="customStyleSelectBox"><span class="customStyleSelectBoxInner">'+html+'</span></span>').css({position:'absolute', opacity:0,fontSize:$(this).next().css('font-size')}); 
     var selectBoxSpan = $(this).next(); 
     var selectBoxWidth = parseInt($(this).width()) - parseInt(selectBoxSpan.css('padding-left')) -parseInt(selectBoxSpan.css('padding-right'));   
     var selectBoxSpanInner = selectBoxSpan.find(':first-child'); 
     selectBoxSpan.css({display:'inline-block'}); 
     selectBoxSpanInner.css({width:selectBoxWidth, display:'inline-block'}); 
     var selectBoxHeight = parseInt(selectBoxSpan.height()) + parseInt(selectBoxSpan.css('padding-top')) + parseInt(selectBoxSpan.css('padding-bottom')); 
     $(this).height(selectBoxHeight).change(function(){ 
     selectBoxSpanInner.text($(this).find(':selected').text()).parent().addClass('changed'); 
     }); 
    }); 
    } 
} 
}); 
$('select').customSelect(); 

而且它的確定,它的偉大工程,問題是,我也有,如果你點擊廣告,其越來越多的選擇框,只要你想盡可能多的按鈕(沒關係原因:P)。 這些選擇框也是在jquery的幫助下創建的,所以我將它動態添加到dom中。

所以我的問題是,這些新的選擇框當然不會繼承樣式,我知道這是因爲我以後將它們添加到DOM,但是我可以讓它們繼承on方法的幫助?

謝謝,任何幫助!

+0

我不知道你使用的是什麼版本的jQuery,但似乎值得注意的是['.browser'已被棄用](http://api.jquery.com/jquery.browser/)。 – 2012-04-26 11:06:15

回答

1

你只需要調用再次

$('select.newSelect').customSelect(); 

你添加新的選擇到DOM後。小心添加一個特殊的類,以便您的customSelect()代碼僅適用於新添加的元素,否則您將附加事件兩次。礦石則可以將插件使得它不重視的事件兩次,但那是比較困難(提示使用.data()

避免做奇怪的事情,如果你打電話$('select.newSelect').customSelect();你可以做

if(!$.browser.msie || ($.browser.msie&&$.browser.version>6)){ 
    return this.each(function() { 

    if($(this).data('customSelectApplied') !== ''){ 
     // whe already attached the custom functionality here 
     return; 
    }else{ 
     //continue as usual 
     $(this).data('customSelectApplied', true); 
+0

感謝@Nicola Peluchetti,但問題是,它弄亂了我的第一個選擇框,我的意思是它創建了另一個,因爲當然我再次調用函數 – 2012-04-26 10:55:12

+0

@ Mr.Sam您必須添加一個自定義類到您的新添加選擇框或更改插件 – 2012-04-26 10:56:04

+0

我看到了,但我無法想象如果有人會產生大量的選擇框會發生什麼 – 2012-04-26 10:57:53

0

on功能在這裏不會幫助你。只需要調用該函數每次添加新elemenes:

$("<select id="aaa">").appendTo('#foo').customSelect(); 

如果你想虎視眈眈在一個查詢新的 「選擇」 S:
做這在DOM準備:

$(function(){ 
    $('select').customSelect().addClass('old'); 
}); 

後來就

$('select:not(.old)').customSelect().addClass('old');  

您還可以更改插件添加該類而忽略與old類元素:

$.fn.extend({ 
customSelect : function(options) { 

    if(!$.browser.msie || ($.browser.msie&&$.browser.version>6)){ 
    return this.filter(':not(.old)').each(function() { // <==== ====== ===== ====== === 
     $(this).addClass('old'); // <==== ====== ===== ===== ==== 
     var outerClass = options.customClass, 
      innerClass = options.customClass_inner; 

     var currentSelected = $(this).find(':selected'); 
     var html = currentSelected.html(); 
     if(!html){ html='&nbsp;'; } 
     $(this).after('<span class="customStyleSelectBox"><span class="customStyleSelectBoxInner">'+html+'</span></span>').css({position:'absolute', opacity:0,fontSize:$(this).next().css('font-size')}); 
     var selectBoxSpan = $(this).next(); 
     var selectBoxWidth = parseInt($(this).width()) - parseInt(selectBoxSpan.css('padding-left')) -parseInt(selectBoxSpan.css('padding-right'));   
     var selectBoxSpanInner = selectBoxSpan.find(':first-child'); 
     selectBoxSpan.css({display:'inline-block'}); 
     selectBoxSpanInner.css({width:selectBoxWidth, display:'inline-block'}); 
     var selectBoxHeight = parseInt(selectBoxSpan.height()) + parseInt(selectBoxSpan.css('padding-top')) + parseInt(selectBoxSpan.css('padding-bottom')); 
     $(this).height(selectBoxHeight).change(function(){ 
     selectBoxSpanInner.text($(this).find(':selected').text()).parent().addClass('changed'); 
     }); 
    }); 
    } 
} 
}); 

然後你只需使用它像這樣:

$('select').customSelect(); 

而且它會忽略「老「<select> s。

+0

謝謝,但實際上我希望有人能告訴我,當我向dom中添加新元素時,我該如何繼續,在這種情況下,新的表單元素,並使他們都繼承任何我添加它們之前添加它們。因爲我不只是選擇框,我有一個洞用戶字段,其中有文本輸入,選擇框,等等......所有這些元素都需要繼承東西。所以我正在尋找答案,我可以讓他們繼承,這可能嗎? – 2012-04-26 11:03:46

+1

@ Mr.Sam。那麼答案很簡單。沒有。 **:)** – gdoron 2012-04-26 11:05:20

+0

@ Mr.Sam。但更新插件就像我顯示你可能會有所幫助,是嗎? – gdoron 2012-04-26 11:06:22