2014-11-03 170 views
0

元件的負載我搜索了一段時間,但找不到我的問題一個有效的解決方案(對不起,如果我搜索錯誤)...... 我想有以下功能的一個等價元件裝載有AJAX:jQuery的 - 與阿賈克斯

$(document).ready(function() { 
    $('.div-modal-text').css("background-color","red"); 
}); 

因此我使用以下代碼:

$('#parent_id').on('action', '#id or class to be born', function to be performed()); 

在我的例子的父子結構是:#projectModals(DIV其內容是動態加載)> .. 。>。div-project-description> .div-modal-text

這相當於在我的例子下面的代碼:

$('#projectModals').on('load', '.div-project-description', function() { 
     $(this).find('.div-modal-text').each(function() { 
      $(this).css("background-color","red"); 

    }); 
    }); 

我知道「負荷」不工作。對(),但我嘗試了不同的選擇,我無法找到任何工作。

感謝您的幫助!

編輯:這是我的(簡單)的HTML代碼:

<div id="projectModals"></div> 

內容加載#projectModals使用Ajax:

 <div class="row"> 
     <div class="div-project-idcard">   
      column 1, don't bother too much about this column    
     </div> 
     <div class="div-project-description"> 
      <div id="summary" class="div-modal-text"> 
      Column2, text 1    
      </div> 
      <div id="purpose" class="div-modal-text""> 
      Column2, text 2 
      </div> 
      <div id="reforestation" class="div-modal-text"> 
      Column2, text 3 
      </div> 
      <div id="certification" class="div-modal-text"> 
      Column2, text 4 
      </div> 
     </div>   
     </div> 
+0

你會分享你的html代碼嗎? – 2014-11-03 08:18:58

+0

你是否總是需要'.div-modal-text'的相同背景顏色?如果是這樣,爲什麼不只使用CSS? – 2014-11-03 08:20:56

+0

如何將您的內容加載到#projectModals中,發佈該腳本 – SarathSprakash 2014-11-03 08:28:07

回答

0

如果你在等待它通過加載ajax,考慮用$(window).ajaxSuccess()代替:

$(window).ajaxSuccess(function(){ 
    $('#projectModals .div-project-description .div-modal-text').each(function() { 
     $(this).css("background-color","red"); 
    }); 
    }); 
+0

而不是'$('#projectModals.div-project-description .div-modal-text').css(「background-color」,「red」)' – 2014-11-03 08:26:02

+0

謝謝,那是一個錯字。現在修復。 – Scimonster 2014-11-03 08:26:41

0

據我所知,你想在ajax執行後改變一些背景顏色。

我認爲這是最好的,使在阿賈克斯成功

$.ajax({ 
    url: "some url", 
    data: {some data}, 
    success: onAjaxSuccess, 
}); 

然後操作;

function onAjaxSuccess(data){ 
    //.. do things with data 
    var toExecute = $('#projectModals .div-project-description'); 
    toExecute.find('.div-modal-text').each(function() { 
      $(this).css("background-color","red"); 
    }); 
}