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

时间:2015-06-02 19:03:39

标签: javascript jquery dom toggle

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

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>

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

1 个答案:

答案 0 :(得分:1)

尝试

&#13;
&#13;
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);
&#13;
li,
ol {
  list-style: none;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="regionContent">
  <ol></ol>
</div>
&#13;
&#13;
&#13;