Jquery slideDown child ul
我怎样才能只滑下正在盘旋的元素的孩子ul?
Jquery $(document).ready(function() { $(".sub").hide(); $(".main").hover(function() { $(".sub").slideDown('slow'); }); });
添加上下文:
$(".sub", this).slideDown('slow');
选择器的第二个参数是上下文。 通过传递this
,我们告诉选择器只匹配在其中找到的.sub
元素,其中, this
表示您当前正在hover的.main
。
相当于:
$(this).find('.sub').slideDown('slow');
更多信息,请访问http://api.jquery.com/jQuery/#selector-context
$(".main").hover(function() { $(".sub", this).slideDown('slow'); });
$(document).ready(function() { $(".sub").hide(); $(".main").hover(function() { $(this).find(".sub").slideDown('slow'); }); });
this
指的是hover的.main
和.find
会找到其中的所有.sub
元素。
我想我理解你的问题。 在这种情况下,您可以使用更多选择器或查找jQuery的最后一个函数。
CSS:
.sub { display: none; /* probably you will need height: 0; and/or position relative; left: Xpx; top: Ypx; to make the sub-menus at good position for showing.. */ }
JavaScript的:
function hideMenu(obj) { // This is valid. Depending on situation you will see below another sample. $(obj).find("ul").slideUp(); // or // $(obj + " ul").slideUp(); } function showMenu(obj) { // This is valid. Depending on situation you will see below another sample. $(obj).find("ul").slideDown(); // or // $(obj + " ul").slideDown(); }
我想你正在生成整个页面……在这种情况下,你必须以相同的方式生成li元素,但添加onmouseover =“showMenu(this)”onmouseout =“hideMenu(this)”:
...
你必须在你的html中包含JavaScript文件 – 在head标签中:
这是一个示意图。