jquery
  • bind
  • wymeditor
  • 2011-09-10 42 views 0 likes 
    0

    我有這樣的代碼 -JQuery的.bind()格式正確

    jQuery('.wymeditor').wymeditor({ 
        html: '<p>Hello, World!<\/p>', 
        postInit: function(wym) { 
         var html = "<li class='wym_tools_newbutton'>" 
           + "<a name='NewButton' href='#'" 
           + " style='background-image:" 
           + " url(js/wymeditor/img/media.png);'" 
           + " title='Insert an image' >" 
           + "</a></li>"; 
         jQuery(wym._box).find(wym._options.toolsSelector + wym._options.toolsListSelector).append(html); 
         jQuery(wym._box).find('li.wym_tools_newbutton a').click(function() { 
    
         jQuery('#modal').show().css({ 'left': (winW-980)/2+'px', 'top': '100px' }).load('admin_list_media.php'); 
    
           jQuery('.imgname').bind('change',function(){ 
    
           alert('123'); 
    
           var InsertImg = '#'+jQuery(this).attr('id'); 
    
           wym.insert('<img src="uploads/'+jQuery(InsertImg).val()+'" />'); 
    
           jQuery('#modal').empty().hide(); 
    
          }); 
          return(false); 
          }); 
          } 
         }); 
    

    它新增了一個按鈕,一個wym編輯器。這將打開一個包含圖像的模式,這個想法是選擇一個圖像並插入到wym編輯器中。如果我使用jQuery('.imgname').live('change',function(){ ...,但是如果我使用jQuery('.imgname').bind('change',function(){,則不適用問題是我必須使用.bind(),因爲每次打開模式時都會綁定更改事件處理程序,所以每次我被告知要替換.live()與.bind()但它不起作用(在我的代碼中確定)。建議請

    回答

    0

    通過將插入件移動到更換部分外部解決它

    1

    首先:爲什麼你不使用live但是這樣做一次當你的頁面加載?你說「我必須使用bind,因爲事件處理程序每​​次都會被綁定......」但這聽起來令人困惑。由於您使用bind,所以事件處理程序每​​次都會綁定,而不是相反。

    做不到這一點,一個快速和骯髒的解決辦法是:

    $(".imgname").unbind("change").bind("change", function() { 
        // ... 
    }); 
    

    更好的解決方案(同樣,沒有live)將是hasEventListener插件。閱讀文檔,或使用其sandbox on JSFiddle

    相關問題