简体   繁体   English

jQuery / JavaScript:如何删除第一个中的所有元素 <li> <a>文字</a>除外

[英]jQuery/JavaScript: how to remove all elements in the first <li> except <a>text</a>

I have a nested list. 我有一个嵌套列表。

<ul id="menu-main-menu" class="nav sn-nav">
   <li id="menu-item-38" class="menu-item menu-item-type-yawp_wim menu-item-object-yawp_wim menu-item-38">              
      <div class="yawp_wim_wrap">
         <div class="widget-area">
            <div id="custommetawidget-3" class="yawp_wim_widget customMetaWidget">  
               <span class="yawp_wim_title"></span> 
               <ul>
                  <li><a href="1">Site Admin</a></li>
                  <li><a href="2">Log out</a></li>
               </ul>
            </div>
         </div>
       </div>
    </li>
    <li>
       <a href="3">test</a>
    </li>
</ul>

I want to remove all elements of first <li id='menu-item-38'> but leave <a> . 我想删除第一个<li id='menu-item-38'>所有元素,但保留<a>
Just like the second <li> . 就像第二个<li>
I found a similar link: http://jsfiddle.net/yJrb7/1/ 我找到了类似的链接: http : //jsfiddle.net/yJrb7/1/
But it keep not working. 但是它仍然无法正常工作。

var li = document.getElementsByClassName('menu-item')[0];
var links = document.getElementsByTagName('a');
for(var i=0;i<li.childNodes.length;i++){
  if(li.childNodes[i].nodeName!=links)
    li.removeChild(li.childNodes[i--]);
}

EDIT: 编辑:
Hey guys, sorry my fault, I didn't make clear. 大家好,抱歉,我没有说清楚。
What I need actually is 我真正需要的是

<ul id="menu-main-menu" class="nav sn-nav">
  <li><a href="1">Site Admin</a></li>
  <li><a href="2">Log out</a></li>
  <li><a href="3">test</a></li>
</ul>
window.onload = function() {
    //declare an instance of DocumentFragment Type;
    var fragment = document.createDocumentFragment();
    var liList = document.getElementById("menu-item-38").querySelectorAll("li");
    for (var i = 0; i < liList.length; i++) {
        //copy these li into the fragment
        fragment.appendChild(liList[i]);
    }
    var removeObj = document.getElementById("menu-item-38");
    var parentElement = removeObj.parentNode;
    //remove li whose id id [menu-item-38]
    parentElement.removeChild(removeObj);
    //add fragment to the parmentElement
    parentElement.appendChild(fragment);
}
// Selecting the root element
var $li = $('#menu-item-38');
// holding the reference to the elements that you want to keep
var $a = $li.find('a');
// now removing all elements except 'a' elements
$li.find('*:not(a)').remove();
// reattaching them to the root element
$li.append($a);

Hope this helps 希望这可以帮助

Pure JavaScript solution: https://jsfiddle.net/xkchx2f5/ 纯JavaScript解决方案: https : //jsfiddle.net/xkchx2f5/

var item=document.getElementById('menu-item-38');

var list_a=document.getElementsByTagName('a');
var tmp=document.createElement('div');
for(var i=0;i<list_a.length;i++){
    tmp.appendChild(list_a[i].cloneNode(true));
}
item.innerHTML=tmp.innerHTML;

If I'm understanding the question right, couldn't you just set the outerHTML to whatever you want like this? 如果我对问题的理解是正确的,您难道不就将外部HTML设置为您想要的那样吗?

const menu = document.getElementById('menu-main-menu');
const links = menu.querySelectorAll('a');

const items = (x, i) => { 
    const li = document.createElement('li');
    li.innerHTML = links[i].outerHTML;
    return li;
};

const list = Array.from(links).map(items);
menu.innerHTML = list.map(x => x.outerHTML).join('');

You can use following jQuery code. 您可以使用以下jQuery代码。 Just take all a element in a variable and then remove inner html of li tag and the append a tag back to li 只需将所有元素都包含在变量中,然后删除li标签的内部html并将标签附加回li

$(function(){
               var allAtags = $('#menu-item-38 a');
               $("#menu-item-38").html("");
               allAtags.each(function(){
                    $("#menu-item-38").append(this);
               })
            });

I hope it will help 希望对您有所帮助

You can use jQuery unwrap() method for this. 您可以为此使用jQuery unwrap()方法。 The only thing you have to do is give a similar class to all the first direct child of the li, that you want to remove, and pass that class to the unwrap method. 唯一要做的就是为要删除的li的所有第一个直接子对象提供一个相似的类,并将该类传递给unwrap方法。 This will remove all the li from the element that have that similar class. 这将从具有该类似类的元素中删除所有li。

$('.yawp_wim_wrap').unwrap();

this will remove the li from its parent. 这会将li从其父级中删除。 Similiarly you have to add same class to other elements of the other li's that you want to remove. 同样,您必须将相同的类添加到要删除的其他li的其他元素中。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM