【问题标题】:Close HTML list, insert an element, reopen list using jQuery关闭 HTML 列表,插入元素,使用 jQuery 重新打开列表
【发布时间】:2013-07-02 14:48:27
【问题描述】:

首先,我无法访问 HTML。它是由我无法编辑的系统生成的。

我有一个这样的列表:

<ol>
    <li class="odd a one">List Item (type1)</li>
    <li class="even b two">List Item (type1)</li>
    <li class="odd c three">List Item (type1)</li>
    <li class="even d one">List Item (type1)</li>
    <li class="odd a two">List Item (type1)</li>
    <li class="even b three">List Item (type1)</li>
    <li class="odd c one">List Item (type1)</li>
    <li class="even d two">List Item (type2)</li>
    <li class="odd a three">List Item (type2)</li>
    <li class="even b four">List Item (type2)</li>
</ol>

我想使用 jQuery 拆分这个列表并在其中添加一个标题,如下所示:

$('li:contains("type2")').first().before('</ol><h2>Type 2 starts here</h2><ol>')

但是,产生的代码是这样的(根据 Firebug):

<ol>
    <li class="odd a one">List Item (type1)</li>
    <li class="even b two">List Item (type1)</li>
    <li class="odd c three">List Item (type1)</li>
    <li class="even d one">List Item (type1)</li>
    <li class="odd a two">List Item (type1)</li>
    <li class="even b three">List Item (type1)</li>
    <li class="odd c one">List Item (type1)</li>
    <h2>Type 2 starts here</h2>
    <ol></ol>
    <li class="even d two">List Item (type2)</li>
    <li class="odd a three">List Item (type2)</li>
    <li class="even b one">List Item (type2)</li>
</ol>

有没有办法生成以下代码?:

<ol>
    <li class="odd a one">List Item (type1)</li>
    <li class="even b two">List Item (type1)</li>
    <li class="odd c three">List Item (type1)</li>
    <li class="even d one">List Item (type1)</li>
    <li class="odd a two">List Item (type1)</li>
    <li class="even b three">List Item (type1)</li>
    <li class="odd c one">List Item (type1)</li>
</ol>
<h2>Type 2 starts here</h2>
<ol>
    <li class="even d two">List Item (type2)</li>
    <li class="odd a three">List Item (type2)</li>
    <li class="even b one">List Item (type2)</li>
</ol>

这里有一个小提琴:http://jsfiddle.net/a78pT/1/

【问题讨论】:

标签: jquery


【解决方案1】:

大概是这样的吧?

$(document).ready(function () {
    var type1 = $('li:contains("type1")'),
        type2 = $('li:contains("type2")'),
        ol = $('ol'),
        newOL = $('<ol />')
        h2 = $('<h2 />').text('Type 2 starts here');
    ol.empty().append(type1).parent().append(h2).append(newOL.append(type2));
});

工作演示:http://jsfiddle.net/7U3Me/

【讨论】:

  • 值得注意的是,此方法将删除任何添加到“li”元素的 jquery 数据。例如,任何添加的事件都将被删除。
【解决方案2】:

试试这个:

$('li:first').nextUntil('li:contains("type2")').addBack().wrapAll('<ol>');
$('li:contains("type2"):first').nextAll().addBack().wrapAll('<ol>');
$('ol:eq(1)').after('<h2>Type 2 starts here</h2>');
$('li:first').closest('ol').unwrap();

jsFiddle example

这会产生 HTML:

  <ol>
    <li class="odd a one">List Item (type1)</li>
    <li class="even b two">List Item (type1)</li>
    <li class="odd c three">List Item (type1)</li>
    <li class="even d one">List Item (type1)</li>
    <li class="odd a two">List Item (type1)</li>
    <li class="even b three">List Item (type1)</li>
    <li class="odd c one">List Item (type1)</li>
</ol>
<h2>Type 2 starts here</h2>
<ol>
    <li class="even d two">List Item (type2)</li>
    <li class="odd a three">List Item (type2)</li>
    <li class="even b four">List Item (type2)</li>
</ol>

【讨论】:

  • 我也喜欢这个。很高兴看到我错过的所有不同方法(!)
【解决方案3】:

您可以使用分离来删除和重新插入列表项。然后将它们全部附加到父级

http://jsfiddle.net/a78pT/7/

$("ol").parent().append("<h2>Type 2 starts here</h2>");
var newList = $("<ol></ol>");
$(newList).append($('li:contains("type2")').detach());
$("ol").parent().append(newList);

【讨论】:

  • 没问题。但是,接受的答案存在问题。完全重建列表会删除任何以前添加的功能。因此,如果事件附加到任何列表项。重建后甚至会更长。例如:jsfiddle.net/7U3Me/1 点击事件不再生效。但是,使用 detach 可以保护所有功能,例如:jsfiddle.net/a78pT/8
  • 好的,我将对此进行一些实验,因为我可能需要在某些时候将addClass() 的东西发送到lis。谢谢。
  • 任何时候,祝你好运。作为参考,您应该阅读 detach api.jquery.com/detach
【解决方案4】:

我的解决方案可能比@pete 的简单一点。

$('body').append("<h2>Type 2 starts here</h2>");
$('body').append("<ol class='two'>");
$('li:contains("type2")').appendTo("ol.two");

可能需要稍作调整,但这是基础。

演示:http://jsfiddle.net/a78pT/9/

【讨论】:

  • 可能更简单,但也不能满足 OP 的要求。
  • 是的,很有趣。我不希望它们出现在两个列表中,但我知道你要去哪里。感谢您的回答。
  • 哎呀...我想我毕竟不需要clone()。我已经编辑了代码和 jsfiddle。很抱歉误导。
猜你喜欢
  • 1970-01-01
  • 2013-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-12
  • 2015-08-08
  • 2019-12-29
相关资源
最近更新 更多