为菜单列表中的所有链接插入Parent

时间:2017-04-15 17:38:05

标签: javascript html

我正在尝试插入 h2 标记,并使其成为垂直导航菜单中所有链接的父级。

我可以为单个元素执行此操作,但现在我想对菜单中的所有元素执行此操作。

JavaScript的:

var menu_item = document.querySelector("div.righ-nav-style1 ul li a");    

// `menu_item` is the element you want to wrap
var parent = menu_item.parentNode;
var wrapper = document.createElement('h2');
wrapper.className = 'category_links';    

// set the wrapper as child (instead of the 'menu_item')
parent.replaceChild(wrapper, menu_item);
// set 'menu_item' as child of wrapper
wrapper.appendChild(menu_item); 

我想在forloop中为所有菜单项使用 replaceChild() appendChild()方法。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

编辑:我实际上刚刚找到了解决问题的方法。

编辑JavaScript:

var date = new Date('2017-03-16T14:11:48.700000')
date.getMonth()+1  // 3
date.getDate() // 16
date.getFullYear() // 2017
...