如果 ul 有 ul 子级,是否有 CSS 方法来添加箭头?
我的导航结构如下所示:
<div class="menu">
<ul>
<li><a href="#">Page 1</a></li>
<li><a href="#">Page with Subpages</a>
<ul class="children">
<li><a href="#">Page with another subpage</a>
<ul class="children">
<li><a href="#">subsubpage</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#">Page 3</a></li>
<li><a href="#">Page 4</a></li>
</ul>
</div>
我希望所有具有子导航子项的
都应用一个小向下箭头,以便用户知道该页面有子页面。是否可以在纯 CSS 中检测
是否具有 ul.children
的子级?在上面的示例中,“带有子页面的页面”应该应用向下箭头,并且“带有另一个子页面的页面”也应该应用。现在我正在使用 jquery 来解决这个问题:
$(function() {
$('.menu a').each(function() {
if ( $(this).parent('li').children('ul').size() > 0 ) {
$(this).append('<span class="dwn">▼</span>');
}
});
});
Is there a simple pure CSS way to do so?
谢谢。
My navigation structure looks like this:
<div class="menu">
<ul>
<li><a href="#">Page 1</a></li>
<li><a href="#">Page with Subpages</a>
<ul class="children">
<li><a href="#">Page with another subpage</a>
<ul class="children">
<li><a href="#">subsubpage</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#">Page 3</a></li>
<li><a href="#">Page 4</a></li>
</ul>
</div>
I want all <li>
's that have a subnavigation children to have a little down-arrow applied so users know this page has subpages.
Is it possible to detect in pure css if a <li>
has children of ul.children
? In my example above, the "Page with Subpages" should have the down arrow applied and the "Page with another subpage" as well.
Right now I'm using jquery to solve this problem:
$(function() {
$('.menu a').each(function() {
if ( $(this).parent('li').children('ul').size() > 0 ) {
$(this).append('<span class="dwn">▼</span>');
}
});
});
Is there an easy pure CSS way to do so?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
这在CSS中是完全可行的——
你的结构是这样的:
你的CSS将是这样的——
如果你想要一个下拉菜单,其中顶级项目上有一个向下箭头,然后向右,则更进一步子菜单的箭头,你的 CSS 看起来像这样
这是一个正在运行的 jsfiddle - http://jsfiddle.net/w9xnv/2 /
This is perfectly doable in CSS --
Your structure is this:
Your CSS will be like this --
Taking it one step farther, if you want to have a drop down menu where there is a down arrow on the top level items and then right arrows for sub menus, your CSS would look like this
Here is a jsfiddle of it in action - http://jsfiddle.net/w9xnv/2/
你可以这样做:
这是一个示例
这个没有所有浏览器都支持,这里是支持列表
编辑< /strong>
刚刚意识到我上面的例子会有缺陷,箭头将位于错误的位置。不过,如果这是您想要追求的途径,则可以正确对齐箭头。
You could do this:
here is an example
This doesn't have support in all browsers, here is a list of support
EDIT
Just realised that my example above will be flawed, the arrow will be in the wrong position. Still - if this is an avenue you would like to pursue the possibility is there to align the arrow correctly.
老问题,但这就是我要做的:
你也可以让它看起来更好,如下所示:
Old question but this is how I would do it:
You can also make it look nicer like this:
对于未来的读者!我也想知道,然后自己想通了,
我们无法检查元素是否有子元素,但我们可以检查它是否为空
For future readers! I was wondering too and then figured it out on my own
we can't check if element has children, but we can check if it is empty
我认为最好的方法是将一个类(或您正在使用的跨度)添加到服务器上的那些
li
中。另一个解决方案,但可能并不容易且不干净,是将箭头添加到所有 ul 并仅使它们在 ul.children 中可见(使用 css)。然后,您可以尝试将箭头放置在父 li 的前面或后面。这是否有效将取决于设计。
I think the best way would be to add a class (or that span you're using) to those
li
s on the server.Another solution, but probably not easy and not clean, is to add the arrow to all
ul
s and only make them visible (using css) in ul.children. You can then try to position the arrow in front of or behind the parent li. Whether this will work will depend on the design.为了保持简单,这是我喜欢使用的解决方案:
在项目名称周围放置标签作为下拉菜单。
使用 CSS 添加箭头:
希望这对您有用!我相信这是我遇到过的最简单的方法!
In the interests of keeping it simple, here is the solution I like to use:
Place tags around the name of the item acting as a drop-down menu.
Add the arrow using CSS:
Hope this works for you! I believe it to be the simplest method I've come across!
没有 CSS“父选择器”。
但这里有一个较短的 jquery 代码:
您的问题与此类似:
活动子级父级的复杂 CSS 选择器
There is no CSS 'parent selector'.
But here is a shorter jquery code:
Your question is similar to this one:
Complex CSS selector for parent of active child
对于垂直菜单:
这个js,适用于所有= nav.vertical li
For vertical menu :
this js, Works on all = nav.vertical li
例如
CSS
For example
and the CSS
这只是 rotaercz 答案的改进。在 CSS 技巧的帮助下
This is just a refinement of rotaerczs answer. With help from CSS Tricks
对于 Wordpress,请使用以下内容
感谢 https:// www.billerickson.net/code/add-arrows-to-menu-items/
For Wordpress Please Use the Following
Thanks to https://www.billerickson.net/code/add-arrows-to-menu-items/