Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 附加到动态元素?_Jquery - Fatal编程技术网

Jquery 附加到动态元素?

Jquery 附加到动态元素?,jquery,Jquery,我有一个数据库中的记录,其中包含每个记录的父子数据。我正在尝试创建父子列表,但在创建子列表时遇到问题: $(document).ready(function() { var objCategories = new Object ({ id: 0 }); $('#load-structures').click(function(event) { event.preventDefault(); objCategories.id

我有一个数据库中的记录,其中包含每个记录的父子数据。我正在尝试创建父子列表,但在创建子列表时遇到问题:

$(document).ready(function() {
    var objCategories = new Object ({
        id: 0
    });
    $('#load-structures').click(function(event) {
        event.preventDefault();
        objCategories.id = $(this).attr("data-category");
        categories();
    });
    function categories() {
        $(".flash").show();
        $(".flash").fadeIn(400).html("Loading...");
            $.ajax({
                url: base_url + "notes/jq_get_structures/" + objCategories.id,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                cache: false,
                success: function (element) {
                    $(".flash").hide();
                    $(".load-link").addClass("link-none");
                    for (var i=0;i<element.length;i++) {
                        if (element[i].parent == 0) {
                            $("#links-structures-parents").append('<li id="structure-parent-' + element[i].structure_id + '"><a href="#structures" title="View ' + element[i].name + '" class="structures">&lsquo;' + element[i].name + '&rsquo;</a></li>');
                        } else if (element[i].parent > 0) {
                            if ($('#structure-children-' + element[i].structure_id).length) {
                                $("#structure-children-" + element[i].structure_id).append('<li id="structure-child-' + element[i].structure_id + '"><a href="#structures" title="View ' + element[i].name + '" class="structures">&lsquo;' + element[i].name + '&rsquo;</a></li>');
                            } else {
                                $("#structure-parent-" + element[i].structure_id).html('<ul id="structure-children-' + element[i].structure_id + '">');
                                $("#structure-parent-" + element[i].structure_id).html('<li id="structure-child-' + element[i].structure_id + '"><a href="#structures" title="View ' + element[i].name + '" class="structures">&lsquo;' + element[i].name + '&rsquo;</a></li>');
                                $("#structure-parent-" + element[i].structure_id).html('</ul>');
                            }
                        }
                    }
                },
                error: function () {
                    $("#links-structures-parents").empty();
                    $("#links-structures-parents").append('<li>There are no Structures.</li>');
                }
            }
        );
    }
});
$(文档).ready(函数(){
var objCategories=新对象({
身份证号码:0
});
$(“#加载结构”)。单击(函数(事件){
event.preventDefault();
objCategories.id=$(this.attr(“数据类别”);
类别();
});
功能类别(){
$(“.flash”).show();
$(“.flash”).fadeIn(400.html(“加载…”);
$.ajax({
url:base_url+“notes/jq_get_structures/”+objCategories.id,
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
cache:false,
成功:功能(元素){
$(“.flash”).hide();
$(“.load link”).addClass(“link none”);
对于(变量i=0;i 0){
if($('#结构子元素-'+元素[i].structure#id).length){
$(“#结构子元素-”+元素[i]。结构子元素id)。追加(“
  • ”); }否则{ $(“#结构父级-”+元素[i].结构id).html(“
      ”); $(“#structure parent-”+元素[i].structure_id).html(“
    • ”); $(“#结构父级-”+元素[i]。结构id.html(“
    ”); } } } }, 错误:函数(){ $(“#链接结构父项”).empty(); $(“#链接结构父项”).append(“
  • 没有结构。
  • ”); } } ); } });
    数据本身没有问题,而是我试图创建子列表的条件部分


    我有一些,虽然就我的一生而言,我不能让它在本地运行,但我希望有人知道诀窍是什么。

    您在else语句中的代码重写了html代码,所以最后一次调用只会使元素具有
    ,您需要在新的部分中添加以前的html代码,或者一次性完成所有工作

    else {
        var eleobj = $("#structure-parent-" + element[i].structure_id);
        eleobj.html('<ul id="structure-children-' + element[i].structure_id + '">');
        eleobj.html(eleobj.html()+'<li id="structure-child-' + element[i].structure_id + '"><a href="#structures" title="View ' + element[i].name + '" class="structures">&lsquo;' + element[i].name + '&rsquo;</a></li>');
        eleobj.html(eleobj.html()+'</ul>');
    
        //Though you could do this in one call
        eleobj.html(
            '<ul id="structure-children-' + element[i].structure_id + '">'+
            '<li id="structure-child-' + element[i].structure_id + '">'+
            '<a href="#structures" title="View ' + element[i].name + '" class="structures">&lsquo;' + element[i].name + '&rsquo;</a>' +
            '</li></ul>'
        );
    }
    
    else{
    var eleobj=$(“#结构父级-”+元素[i]。结构id);
    html('
      ”); html(eleobj.html()++'
    • '); html(eleobj.html()+'
    ); //虽然你可以在一个电话里做到这一点 eleobj.html( “
      ”+ “
    • ”+ '' + “
    ” ); }
    好的,最后是父子id属性的问题:

    $(document).ready(function() {
        var objCategories = new Object ({
            id: 0
        });
        $('#load-structures').click(function(event) {
            event.preventDefault();
            objCategories.id = $(this).attr("data-category");
            categories();
        });
        function categories() {
            $(".flash").show();
            $(".flash").fadeIn(400).html("Loading...");
                $.ajax({
                    url: base_url + "notes/jq_get_structures/" + objCategories.id,
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    cache: false,
                    success: function (element) {
                        $(".flash").hide();
                        $(".load-link").addClass("link-none");
                        for (var i=0;i<element.length;i++) {
                            if (element[i].parent == 0) {
                                $("#links-structures-parents").append('<li id="structure-parent-' + element[i].structure_id + '"><a href="#structures" title="View ' + element[i].name + '" class="structures">&lsquo;' + element[i].name + '&rsquo;</a>');
                            } else if (element[i].parent > 0) {
                                var htmlElementParent = $("#structure-parent-" + element[i].parent);
                                var htmlElementChild = $("#structure-children-" + element[i].parent);
                                var htmlElementChildren = $("ul#structure-children-" + element[i].parent + " li");
                                if (htmlElementChildren.length == 0) {
                                    htmlElementParent.append(
                                        '<ul id="structure-children-' + element[i].parent + '">' +
                                        '<li id="structure-child-' + element[i].structure_id + '">' +
                                        '<a href="#structures" title="View ' + element[i].name + '" class="structures">&lsquo;' + element[i].name + '&rsquo;</a>' +
                                        '</li></ul>'
                                    );
                                } else if (htmlElementChildren.length > 0) {
                                    htmlElementChild.append(
                                        '<li id="structure-child-' + element[i].structure_id + '">' +
                                        '<a href="#structures" title="View ' + element[i].name + '" class="structures">&lsquo;' + element[i].name + '&rsquo;</a>' +
                                        '</li>'
                                    );
                                }
                            }
                        }
                        $("#load-structures").hide();
                    },
                    error: function () {
                        $("#links-structures-parents").empty();
                        $("#links-structures-parents").append('<li>There are no Structures.</li>');
                    }
                }
            );
        }
    });
    
    $(文档).ready(函数(){
    var objCategories=新对象({
    身份证号码:0
    });
    $(“#加载结构”)。单击(函数(事件){
    event.preventDefault();
    objCategories.id=$(this.attr(“数据类别”);
    类别();
    });
    功能类别(){
    $(“.flash”).show();
    $(“.flash”).fadeIn(400.html(“加载…”);
    $.ajax({
    url:base_url+“notes/jq_get_structures/”+objCategories.id,
    contentType:“应用程序/json;字符集=utf-8”,
    数据类型:“json”,
    cache:false,
    成功:功能(元素){
    $(“.flash”).hide();
    $(“.load link”).addClass(“link none”);
    对于(变量i=0;i 0){
    var htmlElementParent=$(“#结构父级-”+元素[i].父级);
    var htmlElementChild=$(“#结构子元素-”+元素[i].parent);
    var htmlElementChildren=$(“ul#结构子项-”+元素[i]。父级+“li”);
    if(htmlElementChildren.length==0){
    htmlElementParent.append(
    “
      ”+ “
    • ”+ '' + “
    ” ); }else if(htmlElementChildren.length>0){ htmlElementChild.append( “
  • ”+ '' + “
  • ” ); } } } $(“#加载结构”).hide(); }, 错误:函数(){ $(“#链接结构父项”).empty(); $(“#链接结构父项”).append(“
  • 没有结构。
  • ”); } } ); } });

    在这里,您将看到我将附加到未编号的列表,这些列表的id属性包含父值,然后将列表项输入到各自的未编号列表中。

    您没有确切说明问题所在,只是您有一个问题。@Anton,它在那里,所以我不确定您是如何看不到的。@PatrickEvans"... 而是我试图创建子列表的条件部分。“我不能创建子列表。@WayneSmallman,你必须告诉JSFIDLE包含jquery,它是左上角的选择框currentl。”