3 回答
TA贡献1818条经验 获得超7个赞
这是您问题的确切解决方案
<script type="text/javascript">
jQuery(function($) {
$(document).ready(function(){
// Create array from h3s found in main content
let nodeList = document.getElementById('content').querySelectorAll('h3');
let list = [];
nodeList.forEach(function(val){
list.push(val.innerHTML)
})
// Create unordered list
var ul = document.createElement('ul');
// Append unordered list to sidebar
document.getElementById('sidemenu-container').getElementsByClassName("wpb_wrapper")[1].appendChild(ul).id = "top-menu";
console.log("list" , list);
// Append list items to unordered list
list.forEach(function(title){
var li = document.createElement('li');
ul.appendChild(li);
var a = document.createElement('a');
li.appendChild(a);
title = title.split(" ").join("-");
var id1 = "#" + title;
$(a).attr('id', id1);
a.innerHTML += title;
});
});
});
</script>
希望它有效。请检查一下 。
TA贡献2039条经验 获得超7个赞
jQuery(function($) {
$(document).ready(function(){
// Create array from h3s found in main content
let nodeList = document.getElementById('content').querySelectorAll('h3');
let list = [];
nodeList.forEach(function(val){
list.push(val.innerHTML)
})
// Create unordered list
var ul = document.createElement('ul');
// Append unordered list to sidebar
document.getElementById('sidemenu-container').getElementsByClassName("wpb_wrapper")[1].appendChild(ul).id = "top-menu";
// Append list items to unordered list
list.forEach(function(title){
var li = document.createElement('li');
var a = document.createElement('a');
var id = title.toLowerCase().split(" ").join('-');
ul.appendChild(li);
a.href = "#" + id;
li.id = id
a.innerHTML += title;
li.appendChild(a);
});
});
});
添加回答
举报