如何获取表头菜单的element的id


How to get ids of elemenent for menu in header?

我正在编辑wordpress主题。(此:Onetone主题)我正在尝试制作一些页面,比如主页,看到主题却没有提供(我已经联系过他们了)。所以在每一页中,我都会有一些<部分>有一个特定的id和一个菜单,每个菜单都有链接。

我已经做了一个证明:

<?php
$sections = array('home','A','B','C','D');// home,A,B,C,D are the ids of the section added manually
foreach ($sections as $section) {
    echo '<li  class="onetone-menuitem"><a class="onetone-menu-link" id="onetone-menu-'.$section.'" href="#'.$section.'" >
 <span>'.$section.'</span></a></li>';
}?>

但正如您所看到的,我无法通过编程方式获得这些部分的id。我该怎么做?谢谢

使用jQuery提取部分id,下面是一个基本的工作示例。您可以将此代码粘贴到php或html文件中,然后从浏览器中调用它。您唯一需要更改的是jquery.js的位置。

<!doctype html>
<head>
<script type='text/javascript' src='http://localhost/lccs/wp-includes/js/jquery/jquery.js?ver=1.11.1'></script>
<style>
.menu-item
{
    display: inline-block;
    padding: 1em;
    background-color: #444;
}
</style>
<script language="javascript">
jQuery(document).ready(function($)
{
        $("section").each(function()
        {
            var menucode="<span class='menu-item'><a href='#"+this.id+"'>Item</a></span>";
            $("#menu-items").append(menucode);
            console.log(menucode);
        });
});
</script>
</head>
<body>
<main id="main" class="site-main" role="main">
<aside class="menu-aside">
<h4>Menu</h4>
<span id="menu-items"></span>
</aside>
<article class='student type-student status-publish hentry'>
</article>
</main>
<h1>Heading</h1>
<section id="seca-1"><h2>Section A</h2>
</section>
<section id="seca-2"><h2>Section B</h2>
</section>
<section id="seca-3"><h2>Section C</h2>
</section>
<section id="seca-4"><h2>Section D</h2>
</section>
</body>
</html>