在两个现有元素之间添加一个新元素

时间:2018-09-22 14:52:59

标签: javascript css

我使用wordpress,并且希望通过自定义方式在现有菜单之间添加新元素。

第一个具有类add,第二个具有

HashSet

这是我尝试过的事情:

public boolean add(E e) {
   return map.put(e, PRESENT)==null;
}

但是,超链接不起作用。有什么办法可以使其工作?另外,使用.btx-navbar-header可使第二个元素转到页面的下一行。

菜单源示例。

#menu-main-menu

1 个答案:

答案 0 :(得分:1)

尝试这样的事情:

 <script>
 function addMenuItem(aText,aUrl,nextTo) {
     var menuItem = document.createElement("LI");
     var menuItemAnchor = document.createElement("A");
     var menuItemAnchorText = document.createTextNode(aText);
     menuItem.appendChild(menuItemAnchor);
     menuItemAnchor.appendChild(menuItemAnchorText);
     menuItemAnchor.href = aUrl;
     var ref = document.querySelector(nextTo);
     ref.parentNode.insertBefore(menuItem, ref.nextSibling);
 }

 // This is when you call the function and give it a URL and the text for the menu. Also you specify after which LI should it append the new menu item
 addMenuItem('Google It!','http://www.google.com','li.menu-item-144');

 </script>

稍后,您可以在菜单onClick,窗口加载或需要它的任何情况下添加更多项。只需这样调用函数:

 addMenuItem('Button Title','Button URL','LI Class');  

让我知道这是否有帮助。而且这不需要jQuery库,因为这是纯JS。