jQuery .append ( function(index, html){... } ) 问题

发布于 2024-09-12 19:34:49 字数 1449 浏览 7 评论 0原文

假设我们有这个未识别 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏了南城 2024-09-19 19:34:49
values = [{word: "lorem", cls: "latin"}, {word: "ipse", cls: "latin"}, {word:"føø",cls:"swedish-chef"}, {word:"bår",cls:"swedish-chef"}],
len    = values.length,
$ctn   = $('<div/>', {
     class:   'another-class-that-occurs-many-times-on-page'
}),
buildarr = [];

while(len--){
    buildarr.push('<span>');
    buildarr.push(values[len].word);
    buildarr.push(values[len].cls);
    buildarr.push('</span>');
}

$ctn.append(buildarr.join('')).appendTo(document.body);

这只是一个建议,就像你可以完成此类任务一样。它假设我们填充数组的顺序是无关紧要的(通过以最快的方式循环,相反的 while)。如果顺序很重要,请使用标准 for 循环

values = [{word: "lorem", cls: "latin"}, {word: "ipse", cls: "latin"}, {word:"føø",cls:"swedish-chef"}, {word:"bår",cls:"swedish-chef"}],
len    = values.length,
$ctn   = $('<div/>', {
     class:   'another-class-that-occurs-many-times-on-page'
}),
buildarr = [];

while(len--){
    buildarr.push('<span>');
    buildarr.push(values[len].word);
    buildarr.push(values[len].cls);
    buildarr.push('</span>');
}

$ctn.append(buildarr.join('')).appendTo(document.body);

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 a standard for loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文