Wrap / Break列表项目为两个ul

时间:2016-03-11 17:32:09

标签: javascript jquery html replace

尝试换行/拆分列表项并通过替换列表项中的文本来添加类,例如下面的示例?

原创Html:

<div class="widget-content">
    <ul>
        <li><span class="label">textone</span> Test Content</li>
        <li><span class="label">texttwo</span> Test Content</li>
        <li><span class="label">texttwo</span> Test Content</li>

        <li><span class="label">texttwo</span> Test Content</li>
        <li><span class="label">textone</span> Test Content</li>
        <li><span class="label">textone</span> Test Content</li>
    </ul>
</div>

我试图像这样制作输出:

<div class="widget-content">
    <ul class="textone">
        <li><span class="label"> textone</span> Test Content</li>
        <li><span class="label"> textone</span> Test Content</li>
        <li><span class="label"> textone</span> Test Content</li>

    </ul> <!-- Break part 1 -->

    <ul class="texttwo">
        <!-- Break part 2 -->
        <li><span class="label"> texttwo</span> Test Content</li>
        <li><span class="label"> texttwo</span> Test Content</li>
        <li><span class="label"> texttwo</span> Test Content</li>
    </ul>
</div>

我正在尝试将其拆分为多个组,并通过替换ul

中的文本将类添加到新父class="label"(输出Html示例)

以下是我的尝试:

var arr = []; // array to store `li` elements
var widget = $(".widget-content");

$(".widget-content li").each(function(index, el) {
  var html = el.outerHTML;
  // number at `label` text
  var curr = $(".label", this).text().trim().slice(-1);
  if (arr[curr - 1]) {
    arr[curr - 1].push(html)
  } else {
    arr[curr - 1] = [html];
  }
});

widget.html(""); // remove existing `html`

$.each(arr, function(key, val) {
  var li = val.join("");
  $(".widget-content").append($("<ul/>", {
    html: li,
    "class":$(li).eq(0).find(".label").text().trim()
  }))
})

不工作,请检查jsfiddle
提前谢谢。

3 个答案:

答案 0 :(得分:2)

这是一个解决方案,根据.label

中的文字将LI移动到新的父母身上

&#13;
&#13;
var widget  = $('.widget-content'),
    ul      = widget.find('ul'),
    classes = ['left', 'right'];

ul.find('li').each(function() {
    var span   = $(this).find('.label');
    var text   = span.text();
    var parent = widget.find('[data-parent="'+text+'"]');
    
    if ( parent.length === 0 ) {
    	widget.append( $('<ul class="'+classes.shift()+'" data-parent="'+text+'" />').append(this) )
    } else {
    	parent.append(this)
    }
    ul.remove();
});
&#13;
.left  {color: green}
.right {color: blue}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="widget-content">
    <ul>
        <li><span class="label">textone</span> Test Content</li>
        <li><span class="label">texttwo</span> Test Content</li>
        <li><span class="label">texttwo</span> Test Content</li>

        <li><span class="label">texttwo</span> Test Content</li>
        <li><span class="label">textone</span> Test Content</li>
        <li><span class="label">textone</span> Test Content</li>
    </ul>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

遵循您的代码(相同原则),这是另一种解决方案:

&#13;
&#13;
var $widget = $('.widget-content');
var elements = {};

$widget.find('li').each(function () {
  var key = $(this).find('span').text();
  // If key is not defined yet, we define an Array
  elements[key] = typeof elements[key] === 'undefined' ? [] : elements[key];
  // Adds the content of the <li>
  elements[key].push($(this).text());
}).parent().remove(); // Clears the <ul>

$.each(elements, function(index, values) {
  var $ul = $('<ul/>').addClass(index);
  $.each(values, function () {
  	$ul.append($('<li/>').text(this));
  });
  $widget.append($ul);
});
&#13;
 .textone {
   color: red;
 }
 
 .texttwo {
   color: blue;
 }
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="widget-content">
    <ul>
        <li><span class="label">textone</span> Test Content</li>
        <li><span class="label">texttwo</span> Test Content</li>
        <li><span class="label">texttwo</span> Test Content</li>

        <li><span class="label">texttwo</span> Test Content</li>
        <li><span class="label">textone</span> Test Content</li>
        <li><span class="label">textone</span> Test Content</li>
    </ul>
</div>
&#13;
&#13;
&#13;

答案 2 :(得分:1)

jquery wrapAll()是另一个短片&amp;甜蜜的方式。

// get unique texts
var uniTxt = [];
$('.label').text(function(index,txt){
  ( $.inArray(txt,uniTxt) > -1) ? null : uniTxt.push( txt ) ;
});

// rearrange items
$(uniTxt).each(function(index, className){
  $('li').filter(':contains('+className+')')
  .unwrap()                             // remove initial parent ul 
  .wrapAll('<ul class='+className+'>'); // wraps similar txt with a Ul & add class
});

See Demo