[英]How to dynamically build/add list elements (ul, ol, li) using jQuery DOM manipulations?
I am programming an AJAX web page (using IE 8) and need to dynamically build a list in jQuery from the data returned. 我正在编写一个AJAX网页(使用IE 8),需要根据返回的数据在jQuery中动态构建一个列表。 Later, I will be converting list to jQuery accordian.
稍后,我将把列表转换为jQuery accordian。
I am also trying to learn the proper way to use these jQuery functions and chaining. 我也在尝试学习使用这些jQuery函数和链接的正确方法。 I am just a jQuery NOOB, but understand JavaScript.
我只是一个jQuery NOOB,但了解JavaScript。 I found a good article on jQuery dom functions: http://www.packtpub.com/article/jquery-1.4-dom-insertion-methods
我发现了一篇关于jQuery dom函数的好文章: http : //www.packtpub.com/article/jquery-1.4-dom-insertion-methods
I want to add as much as possible using the jQuery dom functions, and jQuery chaining, without resorting to HTML source code using text. 我想尽可能多地使用jQuery dom函数和jQuery链接添加,而不是使用文本来使用HTML源代码。 I want to mostly use
.wrap()
, .appendto()
, .attr()
, .text()
, and .parent()
. 我想大多使用
.wrap()
.appendto()
.attr()
.text()
和.parent()
I don't think that the " .attr("class", "CC_CLASS").
is the best way to add a class. 我不认为“
.attr("class", "CC_CLASS").
是添加类的最佳方法。
Given the HTML code: 鉴于HTML代码:
<div id="outputdiv"></div>
Use jQuery dom functions to change it to be the following: 使用jQuery dom函数将其更改为以下内容:
<div id="outputdiv">
<ul id="JJ_ID">
<li> AAA_text </li>
<li id="BB_ID"> BBB_text </li>
<li class="CC_CLASS"> CCC_text </li>
<li id="DD_ID">DDD_text<br/>
<ol id="EE_ID">
<li> FFF_text </li>
<li id="GG_ID"> GGG_text </li>
<li class="HH_CLASS"> HHH_text </li>
</ol>
</li>
</ul>
</div>
Some code I figured out (ignoring the spaces in text). 我想出了一些代码(忽略文本中的空格)。
var aObj = $('<li></li>').text("AAA_text")
var bObj = $('<li></li>').attr("id", "BB_ID").text("BBB_text");
var cObj = $('<li></li>').attr("class", "CC_CLASS").text("CCC_text");
var dObj = $('<li></li>').attr("id", "DD_ID").text("DDD_text");
var fObj = $('<li></li>').text("FFF_text");
var gObj = $('<li></li>').attr("id", "GG_ID").text("GGG_text");
var hObj = $('<li></li>').attr("class", "HH_CLASS").text("HHH_text");
Somehow add (fObj + gObj + hObj) into eObj ? 不知何故将(fObj + gObj + hObj)添加到eObj中?
var eObj = `*something*`.attr("id", "EE_ID").wrap(`*something*`);
Somehow add (aObj + bObj + cObj+ dObj + eObj) into jObj ? 不知何故将(aObj + bObj + cObj + dObj + eObj)添加到jObj中?
var jObj = `*something*`.attr("id", "JJ_ID").wrap(`*something*`);
jObj.appendTo("#xmlOutputId")
The .append
method returns the same container object you called it on -- make use of this to chain methods pleasantly: .append
方法返回您调用它的相同容器对象 - 利用它来愉快地链接方法:
var inner_list = $('<ol/>', {id: "EE_ID" })
.append( $('<li/>', {text: "FFF_text" })
.append( $('<li/>', {id: "GG_ID", text: "GGG_text" })
.append( $('<li/>', {"class": "HH_CLASS", text: "HHH_text" });
var outer_list = $('<ul/>', {id: "JJ_ID" })
.append( $('<li/>', {text: "AAA_text" })
.append( $('<li/>', {id: "BB_ID", text: "BBB_text" })
.append( $('<li/>', {"class": "CC_CLASS", text: "CCC_text" })
.append(
$('<li/>', {id: "DD_ID", text: "DDD_text"})
.append(inner_list)
);
outer_list.appendTo('#xmlOutputId');
You could actually do the entire thing in a single statement with no var
s, but in my opinion that would be getting too ugly. 你实际上可以在没有
var
的单个语句中完成整个事情,但在我看来会变得太难看了。
You can use jQuery template function to do this in a way easily mainainable. 您可以使用jQuery模板函数以易于维护的方式执行此操作。 Check this link: http://api.jquery.com/tmpl/
查看此链接: http : //api.jquery.com/tmpl/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.