繁体   English   中英

删除列表项并将其添加到无序列表

[英]Remove list item and add them to and unordered list

我正在使用以下链接的数量并不总是相同

 <ul>
    <li>link 1</li>
    <li>link 2</li>
    <li>link 3</li>
    <li>link 4</li>
    <li>link 5</li>
    <li>link 6</li>
    <li>link 7</li>
    <li>link 8</li>
 </ul> 

现在,当我拥有超过5个链接时,我需要删除除5个链接以外的所有项并将它们添加到无序列表中

所以我需要得到以下

 <ul>
    <li>link 1</li>
    <li>link 2</li>
    <li>link 3</li>
    <li>link 4</li>
    <li>link 5</li>
    <li>more+</li>
    <ul>
        <li>link 6</li>
        <li>link 7</li>
        <li>link 8</li>
    </ul>
 </ul> 

我只尝试了以下内容,我不知道如何将切片的列表项添加到var中

$('li').slice(5).remove();
$( "ul" ).append( "<li>More +</li> <ul><li>link 6</li></ul>" );

您可以这样:

var slicer=$('li').size()-5;
var removedItems=$('li').slice("-"+slicer); 
$( "ul" ).append( "<li>More +<ul id='moreItems'></ul></li>" );
$( "#moreItems" ).append( removedItems );

slicer var获取ul的超出部分,而removedItems从切片器变量开始获取jQuery元素。

比您可以将它们附加到新列表中。

演示: http//jsfiddle.net/IrvinDominin/UqqJJ/

不要删除切片,否则它将从dom中删除。 首先,像您一样进行切片选择,并将其分配给变量,然后将该变量附加到UL。 然后,您可以从DOM中删除所需的任何内容。

var myItemsGreaterThan5 = $('li').slice(5);
$( "ul" ).append( "<li>More +</li>" + myItemsGreaterThan5 );
myItemsGreaterThan5.remove();

尝试这个:

var $additionalLi = $('li').slice(5).remove();
if ($additionalLi.length) {
    $('<li />', { 'class': 'expand' }).text('More +')
        .append($additionalLi.wrap('<ul></ul>').parent())
        .appendTo('ul');
}

$('ul').on('click', 'li.expand', function() {
    $('ul', this).toggle();
});

您还需要设置子ul元素以开始隐藏:

ul ul {
    display: none;
}

小提琴的例子

删除并重新添加 Class 属性<div>无序列表项</div><div id="text_translate"><p>这是我能够解决的答案。 想要在按钮单击事件从 Hyperlink1 发生到 Hyperlink3 后更改 css class="jsTree-clicked"。 </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-js lang-js prettyprint-override"> $(document).ready(function () { //remove Class $('#myJSTree').find('.jsTree-clicked').removeClass('jsTree-clicked'); //need to do add it to List Item which has Id =3 //check the list item which has id =3 if so add the class to it // It is not a button click event. $('#myJSTree li').each(function (i, li) { console.log('&lt;li&gt; id =' + $(li).attr('id')); var myIdVal = $(li).attr('id'); if (myIdVal == 3) { $(this).addClass('jsTree-clicked'); } }); });</pre><pre class="snippet-code-css lang-css prettyprint-override"> .jsTree-clicked { background-color:red;}</pre><pre class="snippet-code-html lang-html prettyprint-override"> &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt; &lt;div id="myJSTree"&gt; &lt;ul class="nav nav-list"&gt; &lt;li id="1"&gt; &lt;a class="jsTree-clicked" title="Hyperlink1"&gt;HyperLink1&lt;/a&gt; &lt;/li&gt; &lt;li id="2"&gt; &lt;a title="Hyperlink2"&gt;HyperLink2&lt;/a&gt; &lt;/li&gt; &lt;li id="3"&gt; &lt;a title="Hyperlink3"&gt;HyperLink3&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt; &lt;/div&gt;</pre></div></div><p></p><p> 单击超链接时,JsTree 添加一个 class="jsTree-clicked"。 当您导航到不同的节点时,它将删除并重新添加相同的 class 到导航节点。</p><p> 预期的</p><p>我想要一个 function 根据 div 内的 ID 删除给定列表项的 [class="jsTree-clicked"]。 并通过传递 Key ie ID 将 [class="jsTree-clicked"] 重新添加到任何 ListItem。</p><p> 我希望我能够解释我的问题。 谢谢</p><p>我的 JSTree 是第三方开源的。</p></div>

[英]Remove and re-add Class Attribute inside a <div> unordered list item

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将新列表项添加到无序列表 如何将列表项添加到现有的无序列表 从无序列表中删除唯一项 遍历无序列表以添加和删除类 删除并重新添加 Class 属性<div>无序列表项</div><div id="text_translate"><p>这是我能够解决的答案。 想要在按钮单击事件从 Hyperlink1 发生到 Hyperlink3 后更改 css class="jsTree-clicked"。 </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-js lang-js prettyprint-override"> $(document).ready(function () { //remove Class $('#myJSTree').find('.jsTree-clicked').removeClass('jsTree-clicked'); //need to do add it to List Item which has Id =3 //check the list item which has id =3 if so add the class to it // It is not a button click event. $('#myJSTree li').each(function (i, li) { console.log('&lt;li&gt; id =' + $(li).attr('id')); var myIdVal = $(li).attr('id'); if (myIdVal == 3) { $(this).addClass('jsTree-clicked'); } }); });</pre><pre class="snippet-code-css lang-css prettyprint-override"> .jsTree-clicked { background-color:red;}</pre><pre class="snippet-code-html lang-html prettyprint-override"> &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt; &lt;div id="myJSTree"&gt; &lt;ul class="nav nav-list"&gt; &lt;li id="1"&gt; &lt;a class="jsTree-clicked" title="Hyperlink1"&gt;HyperLink1&lt;/a&gt; &lt;/li&gt; &lt;li id="2"&gt; &lt;a title="Hyperlink2"&gt;HyperLink2&lt;/a&gt; &lt;/li&gt; &lt;li id="3"&gt; &lt;a title="Hyperlink3"&gt;HyperLink3&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt; &lt;/div&gt;</pre></div></div><p></p><p> 单击超链接时,JsTree 添加一个 class="jsTree-clicked"。 当您导航到不同的节点时,它将删除并重新添加相同的 class 到导航节点。</p><p> 预期的</p><p>我想要一个 function 根据 div 内的 ID 删除给定列表项的 [class="jsTree-clicked"]。 并通过传递 Key ie ID 将 [class="jsTree-clicked"] 重新添加到任何 ListItem。</p><p> 我希望我能够解释我的问题。 谢谢</p><p>我的 JSTree 是第三方开源的。</p></div> jQuery - 如何动态地将列表项添加到无序列表? 无序列表-列表项位置 替换无序列表中的列表项 滚动无序列表到最新项目 添加/删除类并使用jQuery附加到无序列表导航
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM