【问题标题】:Insert element between elements within `this` in loop在循环中的`this`中的元素之间插入元素
【发布时间】:2014-07-04 11:08:01
【问题描述】:

我有一个each 循环,其中每个循环元素都具有以下 html 结构:

<li>
    <p>Text</p>
    <form>...</form>
</li>

在循环中,我创建了一个&lt;span&gt; 元素,我想将它插入到&lt;p&gt;&lt;form&gt; 元素之间的每个循环元素中。但我不知道如何访问this 中的元素,我只知道如何在li 的开头(prepend)和结尾('append')插入它,但不在@987654331 之间@ 和&lt;form&gt;

这是我的代码:

function calculation(elements) {
    $(elements).each(function() {
        ...
        var output = $('<span>').addClass('durationValue').html('Some Values..');
        $(this).append(output);
    });
}

【问题讨论】:

标签: jquery html loops this each


【解决方案1】:

您可以使用.after().before() 代替.append().prepend(),如下所示:

$(this).find("form").before(output);

【讨论】:

  • 非常感谢!我不知道我可以在this 上下文中使用类似find() 的东西。 :)
猜你喜欢
  • 2017-03-01
  • 2016-12-12
  • 1970-01-01
  • 2011-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-14
  • 2011-06-16
相关资源
最近更新 更多