简体   繁体   English

语义正确的HTML网站地图

[英]Semantically correct sitemap in HTML

I am trying to develop/design a main menu/site map for our website. 我正在尝试为我们的网站开发/设计主菜单/站点地图。

The brief is that the menu should look like a directory tree and each item on the menu should either expand to reveal more menu items or link to another page on the site. 简而言之,菜单应该看起来像目录树,并且菜单上的每个项目都应该展开以显示更多菜单项目,或者链接到站点上的另一个页面。

On top of this, every item should have the functionality to be added to the sites "Favourites" application, so that every user can more quickly find items that are buried deep within the menu structure. 最重要的是,每个项目都应具有要添加到站点“收藏夹”应用程序中的功能,以便每个用户可以更快地找到菜单结构中深处的项目。

Because of my insane OCD to make sure that everything is done correctly and to the best possible standards, I am having issues getting my markup to be semantically correct and accessible. 由于我疯狂的OCD来确保一切都正确且以最佳标准完成,因此我遇到了使标记在语义上正确且可访问的问题。

Here's what I've got so far: 到目前为止,这是我得到的:

<ul>
    <li>
        <ul>
            <li>
                <a href="example1.html">Collapse "Menu Item 1"</a>
            </li>
            <li>
                <a href="example2.html">Add "Menu Item 1" to Favourites</a>
            </li>
            <li>
                <a href="example1.html">Menu Item 1</a>
                <ul>
                    <li>
                        <ul>
                            <li>
                                <a href="example3.html">Open "Menu Item 1's
                                First Child"</a>
                            </li>
                            <li>
                                <a href="example4.html">Add "Menu Item 1's
                                First Child" to Favourites</a>
                            </li>
                            <li>
                                <a href="example3.html">Menu Item 1's First 
                                Child</a>
                            </li>
                        </ul>
                    </li>
                    <li>
                        <ul>
                            <li>
                                <a href="example5.html">Open "Menu Item 1's
                                Second Child"</a>
                            </li>
                            <li>
                                <a href="example6.html">Add "Menu Item 1's
                                Second Child" to Favourites</a>
                            </li>
                            <li>
                                <a href="example5.html">Menu Item 1's Second
                                Child</a>
                            </li>
                        </ul>
                    </li>
                </ul>
            </li>
        </ul>
    </li>
    <li>
        <ul>
            <li>
                <a href="example7.html">Expand "Menu Item 2"</a>
            </li>
            <li>
                <a href="example8.html">Add "Menu Item 2" to Favourites</a>
            </li>
            <li>
                <a href="example7.html">Menu Item 2</a>
            </li>
        </ul>
    </li>
    <li>
        <ul>
            <li>
                <a href="example9.html">Open "Menu Item 3"</a>
            </li>
            <li>
                <a href="example10.html">Add "Menu Item 3" to Favourites</a>
            </li>
            <li>
                <a href="example9.html">Menu Item 3</a>
            </li>
        </ul>
    </li>
</ul>

As you can see, things start to get very complicated very quickly. 正如你所看到的,事情开始变得非常复杂非常快。

Is this the best way to convey this information or am I over complicating the matter? 这是传达此信息的最佳方法,还是我使事情复杂化了?

Can you think of a better way for me to do this? 您能为我做一个更好的办法吗?

IMO you're using this list wrong. IMO,您使用此列表有误。 Collapse/Open/Add to favs... these elements don't belong to the tree, but you treat them as if they were part of it. 折叠/打开/添加到收藏夹...这些元素不属于树,但是您将它们视为树的一部分。

Your tree should has following structure: 您的树应具有以下结构:

<ul>
  <li>
    <span>menu item 1<span>
    <ul>
      <li>
        <span>child node 1</span>
      </li>
      <li>
        <span>child node 2</span>
      </li>
    </ul>
  </li>
  ...
</ul>

That's the base of the tree. 那是树的根基。 Now you should add actions (open/add... etc.). 现在,您应该添加操作(打开/添加...等)。 They might by placed as another, independent list after span element. 它们可以作为另一个独立的列表放置在span元素之后。 Then just use class to separate childNodes list from actions list: 然后只需使用class将子节点列表与操作列表分开即可:

...
<li>
  <div>
    <span>menu item 1</span>
    <ul class="actions"> ... </ul>
  </div>
  <ul class="childNodes"> ... </ul>
</li>
...

Well... in theory classes aren't required but it's much easier to handle with classes rather ... ul > li > div > ul selectors etc. 嗯...理论上不需要类,但是用类而不是... ul > li > div > ul选择器等来处理要容易得多。

According to first comment 根据第一条评论

Base functionality of the website shouldn't rely on JavaScript. 网站的基本功能不应依赖JavaScript。 That's why I thing addition of whole tree using JS is bad idea. 这就是为什么我认为使用JS添加整棵树不是一个好主意。 Actions like add to favs should be available without JS, but you may feel free to take control over that action, and overwrite it's functionality. 添加到收藏夹之类的操作应在没有JS的情况下可用,但您可以随意控制该操作并覆盖其功能。 So in HTML you have: 因此,在HTML中,您可以:

<a href="/tree/action/add_to_favs?id=123">Add to favs</a>

But using JS you do something like this (pseudo-code): 但是,使用JS可以执行以下操作(伪代码):

actionLink.addEventListener("click", function...
  var id = take id: 123
  do ajax request here
  return false;
});

It's the best way to provide good availability and functionality at the same time. 这是同时提供良好可用性和功能的最佳方法。

About open/collapse actions. 关于打开/折叠动作。 These requires JS by their nature so they can be added to actions list by JS. 这些就其本质而言需要JS,因此可以将它们添加到JS的动作列表中。 But once again... remember about "non-JS users". 但是,再次……记住“非JS用户”。 HTML/CSS should display a whole tree - JS should collapse its branches. HTML / CSS应该显示整个树-JS应该折叠其分支。

Sometimes I do as per Crozin's answer above but on a large site it can be impossible to load such a large tree (I have built systems for sites with over 100k pages). 有时,我按照上述Crozin的回​​答进行操作,但是在大型站点上可能无法加载这么大的树(我已经为超过10万页的站点构建了系统)。

A hybrid solution is to load in the tree so that the path down to the parent page only contains the current path and then display all the children of the parent. 混合解决方案是加载树,以便到父页面的路径仅包含当前路径,然后显示父页面的所有子节点。 Then add JavaScript behaviour to add additinal parts of the tree. 然后添加JavaScript行为以添加树的其他部分。

Doing things this way means that non JS browsing can completely navigate the site but JS browsing gets the enhance functionality, all without the performance hit of building the entire tree. 以这种方式进行操作意味着非JS浏览可以完全导航该站点,但是JS浏览获得了增强的功能,而所有这些都不会影响构建整个树的性能。

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

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