我遇到了一些问题,试图找出一种在单击 ul li 列表中的帖子中的特定内容时附加动态菜单切换的方法。但是,由于某种原因,它不能很好地工作。
我在想 Each(); 和最近();+ 查找();在 jQuery 中。
这是我想要实现的图片:
欢迎任何帮助!谢谢,希望解决方案可以是动态的。
代码:
var activeClass = 'openToggler', showingDropdown, showingMenu, showingParent;
/* hides the current menu */
var hideMenu = function () {
if (showingDropdown) {
showingDropdown.removeClass(activeClass);
showingMenu.hide();
}
};
/* recurse through dropdown menus */
$('.micro-post').each(function () {
/* track elements: menu, parent */
var dropdown = $(this);
//var opts = dropdown.closest("opts");
//console.log(opts);
var menu = dropdown.next('.dropdown-menu'), parent = dropdown.parent();
/* function that shows THIS menu */
var showMenu = function () {
hideMenu();
showingDropdown = $(this).closest('.micro-post').addClass(activeClass);
showingMenu = menu.show();
showingParent = parent;
};
/* function to show menu when clicked */
dropdown.bind('click', function (e) {
if (e) e.stopPropagation();
if (e) e.preventDefault();
showMenu();
});
/* function to show menu when someone tabs to the box */
dropdown.bind('focus', function () {
showMenu();
});
});
/* hide when clicked outside */
$(document.body, ".micro-post").bind('click', function (e) {
if (showingParent) {
var parentElement = showingParent[0];
if (!$.contains(parentElement, e.target) || !parentElement == e.target) {
hideMenu();
}
}
});