【发布时间】:2016-07-02 05:14:17
【问题描述】:
目前我在我的 CSS 中使用 :hover 在 .wrapper1 元素中显示我的嵌套列表。我想让它们在点击而不是悬停时打开。
这是我迄今为止尝试过的:
$(function() {
// whenever we hover over a menu item that has a submenu
$('li.parent').on('click', function() {
var $menuItem = $(this),
$submenuWrapper = $('> .wrapper', $menuItem);
// grab the menu item's position relative to its positioned parent
var menuItemPos = $menuItem.position();
// place the submenu in the correct position relevant to the menu item
$submenuWrapper.css({
top: menuItemPos.top,
left: menuItemPos.left + Math.round($menuItem.outerWidth() * 0.75)
});
});
});
演示:https://jsfiddle.net/72tnxh45/2/
下面是可以影响显示所有子链接的css。
.wrapper1 li:hover > .wrapper1 {
display: block;
}
【问题讨论】:
-
对于初学者,您需要导入 jQuery。这是updated fiddle
-
在我的代码中我包含 jquery。
标签: javascript jquery drop-down-menu html-lists