我有以下我编写的代码,我个人认为它非常好。它需要 a<ul>
并在单击时下拉内容。但是我在理解上存在脱节,我必须做我认为是“肮脏的黑客”来解决的问题。
问题是我不希望类“sidebar-dropdown-open”在插件中如此“硬连线”。但是我发现两者之间有非常明显的区别......
$('.sidebar-dropdown-open')
甚至. 'sidebar-dropdown-open
_'.sidebar-dropdown-open
我通过在我的插件中包含两个不同的“参数”来“解决”这个问题,但我想知道是否有人可以让我了解如何更好地执行此操作,以及为什么会这样。
接线(文件加载)
$(document).ready(function () {
$('[data-role="sidebar-dropdown"]').drawer({
open: 'sidebar-dropdown-open',
css: '.sidebar-dropdown-open'
});
});
html
<ul>
<li class=" dropdown" data-role="sidebar-dropdown">
<a href="pages/.." class="remote">Link Text</a>
<ul class="sub-menu light sidebar-dropdown-menu">
<li><a class="remote" href="pages/...">Link Text</a></li>
<li><a class="remote" href="pages/...">Link Text</a></li>
<li><a class="remote" href="pages/...">Link Text</a></li>
</ul>
</li>
</ul>
javascript
(function ($) {
$.fn.drawer = function (options) {
// Create some defaults, extending them with any options that were provided
var settings = $.extend({
open: 'open',
css: '.open'
}, options);
return this.each(function () {
$(this).on('click', function (e) {
// slide up all open dropdown menus
$(settings.css).not($(this)).each(function () {
$(this).removeClass(settings.open);
// retrieve the appropriate menu item
var $menu = $(this).children(".dropdown-menu, .sidebar-dropdown-menu");
// slide down the one clicked on.
$menu.slideUp('fast');
$menu.removeClass('active');
});
// mark this menu as open
$(this).addClass(settings.open);
// retrieve the appropriate menu item
var $menu = $(this).children(".dropdown-menu, .sidebar-dropdown-menu");
// slide down the one clicked on.
$menu.slideDown(100);
$menu.addClass('active');
e.preventDefault();
e.stopPropagation();
}).on("mouseleave", function () {
$(this).children(".dropdown-menu").hide().delay(300);
});
})
};
})(jQuery);