首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在动态嵌套元素(JavaScript)中分配级联ID

在动态嵌套元素(JavaScript)中分配级联ID
EN

Stack Overflow用户
提问于 2018-09-03 18:58:29
回答 1查看 35关注 0票数 0

我正在开发一个脚本,动态生成3个不同的组件来构建网格:节->行->模块。现在,我正在处理更新函数,它应该能够在创建新组件之后更新网格中每个元素的I:

代码语言:javascript
复制
function Update() {

    // Define components variables
    var sections = document.querySelectorAll('#canvas [data-component="section"]');
    var rows = document.querySelectorAll('#canvas [data-component="row"]');
    var modules = document.querySelectorAll('#canvas [data-component="module"]');

    /**
     * Assign IDs to each existing section, row and module
     */
    // If there are sections...
    if ( sections.length > 0 ) {

        for ( var x = 0; x < sections.length; x++ ) {

            sectionNum = x + 1; 
            sections[x].id = 'component-' + sectionNum;

            // If there are rows...
            if ( rows.length > 0 ) {

                for ( var y = 0; y < rows.length; y++ ) {

                    // If this row is a descendant of that section...
                    if ( rows[y].parentElement.parentElement == sections[x] ) {

                        rowNum = y + 1; 
                        rows[y].id = 'component-' + sectionNum + '-' + rowNum;

                        // If there are modules...
                        if ( modules.length > 0 ) {

                            for ( var z = 0; z < modules.length; z++ ) {

                                // If this module is a descendant of that row...
                                if ( modules[z].parentElement.parentElement == rows[y] ) {

                                    moduleNum = z + 1;
                                    modules[z].id = 'component-' + sectionNum + '-' + rowNum + '-' + moduleNum;
                                };
                            };

                            // If the loop has reached the end, reset the index and break
                            if ( modules.length - 1 === z ) { z = 0; break };
                        };
                    };

                    // If the loop has reached the end, reset the index and break
                    if ( rows.length - 1 === y ) { y = 0; break; };
                };
            };

            // If the loop has reached the end, reset the index and break
            if ( sections.length - 1 === x ) { x = 0; break; };
        };
    };
};

我觉得我已经接近完成它了,但是我一直在努力寻找产生我所需要的输出的方法。

这就是我现在得到的:

代码语言:javascript
复制
#component-1
    #component-1-1
        #component-1-1-1
        #component-1-1-2
    #component-1-2
        #component-1-2-3
        #component-1-2-4
#component-2
    #component-2-3
        #component-2-3-5
        #component-2-3-6
    #component-2-4
        #component-2-4-7
        #component-2-4-8

但是,我需要重置每个新部分的行号和模块号,如下所示:

代码语言:javascript
复制
#component-1
    #component-1-1
        #component-1-1-1
        #component-1-1-2
    #component-1-2
        #component-1-2-1
        #component-1-2-2
#component-2
    #component-2-1
        #component-2-1-1
        #component-2-1-2
    #component-2-2
        #component-2-2-1
        #component-2-2-2

任何想法都是受欢迎的:)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-06 22:33:00

最后,我再次回答了我自己的问题:P,如果它可能对这里的其他人有帮助的话,它就会出现。

首先,下面是最后的代码:

代码语言:javascript
复制
/**
 * Update Components
 */
function Update() {

    /**
     * Assign IDs to each section, row and module
     */
    // Grab the sections contained in the canvas
    var sections = document.querySelectorAll('#canvas [data-component="section"]');

    if ( sections.length > 0 ) {

        for ( var x = 0; x < sections.length; x++ ) {

            // Increase num by 1 to avoid "0" as first index
            var sectionNum = x + 1;

            // Assign an ID to the current section
            sections[x].id = 'component-' + sectionNum;

            // Grab the rows contained in this section
            var rows = document.querySelectorAll('#' + sections[x].id + ' [data-component="row"]');

            if ( rows.length > 0 ) {

                for ( var y = 0; y < rows.length; y++ ) {

                    // Increase num by 1 to avoid "0" as first index
                    var rowNum = y + 1;

                    // Assign an ID to the current row
                    rows[y].id = 'component-' + sectionNum + '-' + rowNum;

                    // Grab the modules contained in this row
                    var modules = document.querySelectorAll('#' + rows[y].id + ' [data-component="module"]');

                    if ( modules.length > 0 ) {

                        for ( var z = 0; z < modules.length; z++ ) {

                            // Increase num by 1 to avoid "0" as first index
                            var moduleNum = z + 1;
                            // Assign ID to module
                            modules[z].id = 'component-' + sectionNum + '-' + rowNum + '-' + moduleNum;
                        }
                    }
                }
            }
        }
    }
}

最后,我在它的前一个循环中定义了行和模块的变量,这最终做到了这一点(在节循环中定义了行变量,在行循环中定义了模块变量)。如果您想知道原因,那是因为这样做的话,我就能够使用当前父程序的ID来将搜索限制在该特定父服务器所包含的子节点上,然后在新父节点被循环时重新启动计数。

这是次要的细节,但我也删除了循环变量重置在每个循环的末尾,这甚至是不必要的。

另外,在jQuery中也有一样的东西:

代码语言:javascript
复制
/**
 * Update Components
 */
function Update() {

    /**
     * Assign IDs to each section, row and module
     */
    // Grab the sections contained in the canvas
    var sections = $('#canvas [data-component="section"]');

    if ( sections.length > 0 ) {

        $(sections).each( function(x) {

            // Increase num by 1 to avoid "0" as first index
            var sectionNum = x + 1;

            // Assign an ID to the current section
            $(this).attr('id', 'component-' + sectionNum);

            // Grab the rows contained in this section
            var thisSectionID = this.id;
            var rows = $('#' + thisSectionID).find('[data-component="row"]');

            if ( rows.length > 0 ) {

                $(rows).each( function(y) {

                    // Increase num by 1 to avoid "0" as first index
                    var rowNum = y + 1;

                    // Assign an ID to the current row
                    $(this).attr('id', 'component-' + sectionNum + '-' + rowNum);

                    // Grab the modules contained in this row
                    var thisRowID = this.id;
                    var modules = $('#' + thisRowID).find('[data-component="module"]');

                    if ( rows.length > 0 ) {

                        $(modules).each( function(z) {

                            // Increase num by 1 to avoid "0" as first index
                            var moduleNum = z + 1;

                            // Assign an ID to the current module
                            $(this).attr('id', 'component-' + sectionNum + '-' + rowNum + '-' + moduleNum);
                        });
                    };
                });
            };
        });
    };

希望它能帮到别人!)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52155038

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档