繁体   English   中英

jQuery-使用Ajax加载元素

[英]jQuery - on load of element with ajax

我搜索了一段时间,但找不到适合我的问题的解决方案(对不起,如果我搜索错误)...对于与ajax一起加载的元素,我希望具有以下功能:

$(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");

    });
  });

我知道'load'不适用于.on(),但是我尝试了其他替代方法,但找不到任何有效的方法。

谢谢你的帮助!

编辑 :这是我的(简体)HTML代码:

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

使用Ajax在#projectModals中加载的内容:

      <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>

如果正在等待通过ajax加载,请考虑使用$(window).ajaxSuccess()代替:

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

据我了解,您想在执行ajax之后更改一些背景颜色。

我认为最好使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");
    });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM