如何选择动态创建的更深层元素?

时间:2015-09-30 11:26:28

标签: jquery

我在ajax脚本中创建了元素:

<?php
...
foreach ($rows as $row) {

   $no_rows = false;

   $img = ($row["img_fichier"] === NULL ? HTTP_IMG.'default_article.jpg' : HTTP_IMG.$row["img_fichier"]);

   $msg .= '<div id="tile_article_'.$row["article_id"].'" data-pk="'.$row["article_id"].'" class="tile fg-black" style="text-align:center;">
                <div class="tile-content">
                    <label class="input-control checkbox small-check">
                        <input class="article" id="article_'.$row["article_id"].'" name="article_'.$row["article_id"].'" type="checkbox" value="'.$row["article_id"].'" />
                        <span class="check"></span>
                        <span class="caption">'.$row["article_lib"].'</span>
                    </label>
                    <input type="text" id="qtecmd_'.$row["article_id"].'" name="qtecmd_'.$row["article_id"].'" placeholder="'._getText('souscommande.qte.placeholder').'" class="numeric qtecmd" />
                    <div class="image-container">
                        <div class="frame"><img src="'.$img.'" /></div>
                    </div>
                </div>
            </div>';
}
...
echo $msg;
}
?>

responseText的{​​{1}}将插入此ajax

div

<div id="cmds"></div> 我致电$(document).ready,因为有ajax个参数来自其他网页:

$_GET

<script> function loadData(page) { var cols = "article_id,article_lib,img_fichier"; var tabs = "article a LEFT JOIN image i ON a.img_id = i.img_id"; var where = ""; if ($("#famille_article").val() != "") { if (where == "") where += "WHERE a.fam_arti_code = '"+$("#famille_article").val()+"'"; else where += " AND a.fam_arti_code = '"+$("#famille_article").val()+"'"; } if ($("#categorie_article").val() != "") { if (where == "") where += "WHERE a.cat_arti_code = '"+$("#categorie_article").val()+"'"; else where += " AND a.cat_arti_code = '"+$("#categorie_article").val()+"'"; } var ret = $.ajax ({ type: "POST", async: false, data: "nbr="+$("#nbr").val()+"&page="+page+"&cols="+cols+"&tab="+tabs+"&where="+where, url: "../../RestaurantCommande/ajaxLoadArticles" }).responseText; $("#cmds").html(ret); } $(document).ready(function() { loadData(1); ... } </script> 我想要隐藏具有班级$(document).ready的元素。怎么做?

2 个答案:

答案 0 :(得分:3)

您可以在插入后执行此操作:

$("#cmds").html(ret).find('.qtecmd').hide();

如果您想直接在$(document).ready中执行此操作,则可以执行以下操作:

$(document).ready(function() {        
    loadData(1);
    $('#cmds').find('.qtecmd').hide();
}

答案 1 :(得分:0)

创建2个名为active和inactive的类,然后通过jquery一旦你想显示它,你可以再次删除非活动和添加活动

&#13;
&#13;
$("#cmds .qtcmds").addClass("inactive");
    $("#cmds .qtcmds").removeClass("inactive").addClass("active");
&#13;
    #cmds .gtecmd.active { display: block;}
    #cmds .gtecmd.inactive { display: none;}
&#13;
&#13;
&#13;