Php 查找子类并将它们分组到父div
我想在每个网页列出的每个母div中找到所有具有id的子类。 这样,子div的id就应该按照mainID进行分组,以便与ajax post一起使用 然而,在jQuery1.11中,我甚至没有得到孩子的id,更不用说用mainID对他们进行分组了 这是迄今为止的代码:Php 查找子类并将它们分组到父div,php,jquery,Php,Jquery,我想在每个网页列出的每个母div中找到所有具有id的子类。 这样,子div的id就应该按照mainID进行分组,以便与ajax post一起使用 然而,在jQuery1.11中,我甚至没有得到孩子的id,更不用说用mainID对他们进行分组了 这是迄今为止的代码: $(".main-column-name").each(function() { var mainID = $(this).attr('id'); var subid = $(this).find(".subdiv-
$(".main-column-name").each(function() {
var mainID = $(this).attr('id');
var subid = $(this).find(".subdiv-name").attr("id");
alert(mainID + ' ' + subid );
});
因此,mainID下应该有一组subID——当然,如果它们存在的话
欢迎任何帮助 您可以将其写入对象数组,并将子数组作为子数组附加到此对象数组
var mainEl = $(".main-column-name")
mainArr = [],
childArr = [];
//set the properties in an array to be used later
mainEl.each(function() {
var id = $(this).attr('id')
children = $(this).find('.subdiv-name').attr('id');
children.each(function() {
//add the child IDs to an array
childArr.push($(this).attr('id'))
});
//push all the properties into an array
mainArr.push({ 'name': id, 'children': childArr });
});
//to access the properties, use a loop
$.each(mainArr, function (i, value) {
//get the name
console.log(mainArr[i].name);
//get the children
console.log(mainArr[i].children);
//and to access the child IDs individually, use this loop within the loop
$.each(mainArr[i].children, function(c, id) {
//get the stored value for id
console.log(id)
});
});
但是要小心使用,因为如果有很多子级和父级,在循环中运行循环可能会导致一些性能问题。除此之外,其他人可能会就提取此信息的更好方法提供建议。。。如果我自己想到一个更好的方法,我会更新,但我没有太多时间来研究它。你知道最接近的方法是什么吗?对不起,我尝试了所有的变体,留下了这个。孩子们等什么都看不出来,所以我有点迷路了。这就是我在这里问它的时候。它选择了最接近的匹配父元素,它似乎不是你想要的。看见这里有所有jQuery的遍历方法。您可能正在寻找
find
方法。谢谢!我现在在那里。但是,我如何将在名为mainID的数组中找到的所有对象分组,然后将其放入ajax数据字符串中呢?