我正在研究一个脚本,该脚本可以动态生成3个不同的组件来构建网格:section-> row-> module。现在,我正在使用更新功能,该功能应该能够在创建新组件之后立即更新网格中存在的每个元素的ID:
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; };
};
};
};
我觉得我已经接近完成,但是我一直在努力寻找产生所需输出的方法。
这就是我现在得到的:
#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
但是我需要在每个新部分重设行号和模块号,如下所示:
#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
任何想法都将受到欢迎:)
答案 0 :(得分:0)
我再一次回答我自己的问题:P,以防它可能对这里的其他人有帮助。
首先,这是最终代码:
/**
* 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;
}
}
}
}
}
}
}
我最终在上一个循环中定义了行和模块的变量,并最终成功了(定义了sections循环中的rows变量和rows循环中的modules变量)。如果您想知道为什么,那是因为这样做,我能够使用当前父代的ID将搜索限制为该特定父代所包含的子代,然后在循环新的父代时重新开始计数。>
这是次要的细节,但是我还删除了每个循环末尾的循环变量重置,甚至没有必要。
另外,这是jQuery中的相同之处:
/**
* 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);
});
};
});
};
});
};
希望它可以帮助某个人! :)