如何使用jquery包装2个动态html元素?
我有以下jquery,我已经创建了动态html结构
var data = [{"name": "Afghanistan", "code": "A"},{"name": "AndorrA", "code": "A"},{"name": "Bouvet Island", "code": "B"},{"name": "Cook Islands", "code": "C"}]; $.each(data, function(key, val) { if (!$("#aZContent ul." + val.code).is("*")) { $("
", { "class": val.code, "html": "" + val.name + " " }) .appendTo("#aZContent ol") .before('' + val.code + ''); } else { $("b." + val.code).each(function() { if (this.textContent === val.code) { $(this).next("ul").append("" + val.name + " "); } }) } });
如何用“li”包装“b”和“ul”元素
尝试使用$.each()
.appendTo()
$.each()
.appendTo()
.append()
; 设置ol li
, b
class
来code
data
属性
$(function() { var data = [{ "name": "Afghanistan", "code": "A" }, { "name": "Bouvet Island", "code": "B" }, { "name": "Cook Islands", "code": "C" }]; $.each(data, function(key, val) { if (!$("#aZContent ul." + val.code).is("*")) { // append `` to `ol` $(" ", { // set `class` to `val.code` "class": val.code, // set `li` `html` to `b` , `ul` , `li` "html": "" + val.code + "" + "- " + val.name + "
" }) .appendTo("#aZContent ol") } else { $("b." + val.code).each(function() { if (this.textContent === val.code) { // append `li` to `ul` $(this).next("ul").append("" + val.name + " "); } }) }; }); });