jQuery .append ( function(index, html){... } ) 问题
假设我们有这个未识别 DIV
<div class="class-occurs-many-times-on-page">foo</div>
,我们想在它后面放置另一个包含数十或数百个 SPAN 元素的未识别 DIV:
<div class="a-class-that-occurs-many-times-on-page">foo</div>
<div class="another-class-that-occurs-many-times-on-page">
<span class="latin">lorem</span><span class="latin">ipse</span>
<span class="swedish-chef">føø</span><span class="swedish-chef">bår</span>
.
.
.
<span>...</span>
</div>
我们已经添加了第一个未识别 DIV,并希望添加 SPAN 容器DIV 以这种方式:
values = [{word: "lorem", cls: "latin"}, {word: "ipse", cls: "latin"},
{word:"føø",cls:"swedish-chef"}, {word:"bår",cls:"swedish-chef"}];
$("#" + someParentElement).append(
$("<div></div>").addClass("a-class-that-occurs-many-times-on-page").text("foo").after(
$("<div></div>").addClass("another-class-that-occurs-many-times-on-page").append(
function(index, html){
// how to wrap each value in the values array in a span
// and inject each of those spans into this DIV?
}
)
)
);
这种方法是否可行,如果可以,如果目标是包装数组中的每个值,则 .append() 方法调用内的函数必须在值数组的每次迭代上执行什么操作在 SPAN 中并将该跨度注入到容器中?
谢谢
Let's say we have this unidentified DIV
<div class="class-occurs-many-times-on-page">foo</div>
and we want to place right after it another unidentified DIV that contains dozens or hundreds of SPAN elements:
<div class="a-class-that-occurs-many-times-on-page">foo</div>
<div class="another-class-that-occurs-many-times-on-page">
<span class="latin">lorem</span><span class="latin">ipse</span>
<span class="swedish-chef">føø</span><span class="swedish-chef">bår</span>
.
.
.
<span>...</span>
</div>
And we have added the first unidentified DIV and want to add the SPAN-container DIV in this way:
values = [{word: "lorem", cls: "latin"}, {word: "ipse", cls: "latin"},
{word:"føø",cls:"swedish-chef"}, {word:"bår",cls:"swedish-chef"}];
$("#" + someParentElement).append(
$("<div></div>").addClass("a-class-that-occurs-many-times-on-page").text("foo").after(
$("<div></div>").addClass("another-class-that-occurs-many-times-on-page").append(
function(index, html){
// how to wrap each value in the values array in a span
// and inject each of those spans into this DIV?
}
)
)
);
Is this approach possible, and if so, what does the function inside the .append() method invocation have to do on each iteration of the values array, if the goal is to wrap each of the values in the array in a SPAN and inject that span into the container?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这只是一个建议,就像你可以完成此类任务一样。它假设我们填充数组的顺序是无关紧要的(通过以最快的方式循环,
相反的 while
)。如果顺序很重要,请使用标准 for 循环
。That is just a suggestion like you could accomplish that kind of task. It assumes the order in which we have to fill the array is irrelavant (by looping with the fastest way,
reversed while
). If the order is important use astandard for loop
.