gpt4 book ai didi

javascript - 如何使用 jquery 展开/折叠 (+/-) 嵌套动态列表

转载 作者:行者123 更新时间:2023-11-28 00:14:19 26 4
gpt4 key购买 nike

我已经尝试过切换功能,但我认为选择正确的列表存在一些问题。

var json ={"Asia": [{"regionList": [{"regionName": "Eastern Asia","Countrylist": [{"countryName": "China","subCountryList": [{"subCountryName": "Southern China"},{"subCountryName": "Eastern China"}]},{"countryName": "Hong Kong"}]},{"regionName": "Southern Asia","Countrylist": [{"countryName": "India"},{"countryName": "Pakistan"}]}]}]};
var html = '';
$.each(json, function(i1, object) {
html += '<li><input type="checkbox" /><b class=' + i1 + ' ><a name="' + i1 + '" >' + i1 + '</a></b>';
$.each(object, function(i2, continent) {
html += '<ul>';
$.each(continent.regionList, function(i3, region) {
html += '<li><input type="checkbox" /><b>' + region.regionName + '</b>';
$.each(region.Countrylist, function(i4, country) {
html += '<ul><li class=' + country.countryName + '><input type="checkbox" />' + country.countryName;
if (country.subCountryList) {
$.each(country.subCountryList, function(i5, subCountry) {
html += '<ul><li><input type="checkbox" />' + subCountry.subCountryName + '</li></ul>';
});
$("b." + country.countryName).toggle(function() {
$(this).children('ul').slideUp();
},
function() {
$(this).children('ul').slideDown();
});
};
html += '</li></ul>';
});
html += '</li>';
});
html += '</ul>';
});
html += '</li>';
});

$('#regionContent ol').html(html);
li, ol{list-style:none;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="regionContent">
<ol></ol>
</div>

我想为包含子国家(中国南部)的国家名称(中国)添加切换功能。在点击中国时,它应该用 +/- 符号折叠所有子国家/地区列表,反之亦然。

最佳答案

尝试

<小时/>

var json = {
"Asia": [{
"regionList": [{
"regionName": "Eastern Asia",
"Countrylist": [{
"countryName": "China",
"subCountryList": [{
"subCountryName": "Southern China"
}, {
"subCountryName": "Eastern China"
}]
}, {
"countryName": "Hong Kong"
}]
}, {
"regionName": "Southern Asia",
"Countrylist": [{
"countryName": "India"
}, {
"countryName": "Pakistan"
}]
}]
}]
};
var html = '';
$.each(json, function(i1, object) {
html += '<li><input type="checkbox" /><b class=' + i1 + ' ><a name="' + i1 + '" >' + i1 + '</a></b>';
$.each(object, function(i2, continent) {
html += '<ul>';
$.each(continent.regionList, function(i3, region) {
html += '<li><input type="checkbox" /><b>' + region.regionName + '</b>';
$.each(region.Countrylist, function(i4, country) {
html += '<ul><li class=' + country.countryName.replace(/\s/g, "-") + '><input type="checkbox" />' + country.countryName;

if (country.hasOwnProperty("subCountryList") && country.subCountryList.length > 0) {
html += '<span class=' + country.countryName.replace(/\s/g, "-") + '>-</span>';
};

$(document)
.on("click", "input ~ span." + country.countryName.replace(/\s/g, "-") + ":first", function(e) {
$(this).siblings("ul").is(":visible") ? $(this).siblings("ul").slideUp({
start: function() {
e.target.textContent = "+"
}
}) : $(this).siblings("ul").slideDown({
start: function() {
e.target.textContent = "-"
}
})
});
if (country.subCountryList) {
$.each(country.subCountryList, function(i5, subCountry) {
html += '<ul><li><input type="checkbox" />' + subCountry.subCountryName + '</li></ul>';

});
};
html += '</li></ul>';
});
html += '</li>';
});
html += '</ul>';
});
html += '</li>';
});

$('#regionContent ol').html(html);
li,
ol {
list-style: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="regionContent">
<ol></ol>
</div>

关于javascript - 如何使用 jquery 展开/折叠 (+/-) 嵌套动态列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30604491/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com