JQUERY 两次点击问题

发布于 2024-10-25 04:01:31 字数 3335 浏览 1 评论 0原文

我目前的菜单有一些问题,根据要求它非常基本。

问题:

用户可以单击菜单按钮一次并打开子菜单,然后用户单击下一个按钮打开该子菜单,根据菜单选项的数量重复此操作。一旦用户单击了它们,如果用户单击另一个菜单按钮一次,它将不会打开,如果用户第二次单击它,它将打开。因此,运行一次后,菜单将仅响应两次用户点击,而不是一次。这个问题也出现在其他领域。

我怀疑这与我的点击处理程序和事件冒泡有关,但我的 JQuery 知识是自学的,因此我可以使用一些指针:

JQuery 代码

$(document).ready(function(){
$('#menu span').hide();
$('#menu li').not('.sub_menu').toggle(function(){
    //$('#menu span').hide();
    $('.open').hide();
    //$(this).next().not('#mm_choice, .sub_menu').show();
    $(this).nextUntil('li').not('#mm_choice, .sub_menu').show().addClass('open');
    }, function(){
    //$(this).next().not('#mm_choice, .sub_menu').hide();
    //$(this).nextUntil('li').not('#mm_choice, .sub_menu').hide().removeClass('open');
    $('.open').removeClass('open').hide();
        });
});

HTML 源代码

<div id="menu" title="Main Menu">
<ul id="m_choices" class="main_menu">

<li id="mm_choice" title="Customer Segmentation Menu">Customer Segmentation</li>

    <span id="c_seg"><li class="sub_menu" title="Cust Seg Menu1">Cust Seg Menu1</li><li id="c_seg" class="sub_menu" title="Cust Seg Menu2">Cust Seg Menu2</li><li id="c_seg" class="sub_menu" title="Cust Seg Menu3">Cust Seg Menu3</li></span>

<li id="mm_choice" title="Industry Menu">Industry Selection</li>

    <span id="all_ind"><li class="sub_menu" title="View All Industries">View All</li></span>

    <span id="spec_ind"><li class="sub_menu" title="View information on a specific Industry">View Specific Industry</li></span>

<li id="mm_choice" title="Customer Satisfaction Menu">Customer Satisfaction</li>

    <span id="all_cs"><li class="sub_menu" title="View every industry's CS survey results">View All</li></span>

    <span id="spec_cs"><li class="sub_menu" title="View a specific industry's CS survey results">View Specific Industry</li></span>

<li id="mm_choice" title="Performance Measures Menu">Performance Measures</li>

    <span id="all_pm"><li class="sub_menu" title="Trend Menu">View All</li></span>

    <span id="spec_pm"><li class="sub_menu" title="Trend Menu">View Specific Industry</li></span>

<li id="mm_choice" title="Business Performance Menu">Business Performance</li>

    <span id="all_bp"><li class="sub_menu" title="Bus Perf Menu">View All</li></span>

    <span id="spec_bp"><li class="sub_menu" title="Bus Perf Menu">View Specific Industry</li></span>

<li id="mm_choice" title="Trend Analysis Menu">Trend Analysis Menu</li>

    <span id="all_trends"><li class="sub_menu" title="Trend Menu">View All</li></span>

    <span id="spec_trends"><li class="sub_menu" title="Trend Menu">View Specific Industry Trends</li></span>  
</ul>
</div>

menu.php 文件(html 标记)被导入(使用 php include();)到 index.php 文件中。 JQuery 文件在 declerations.php 中导入,在 header 中导入到 index.php 中。 Declerations.php 包含所有必需的 JQuery 库。目的是在稍后阶段添加 UI 动画,因此将详细说明 .show() 和 .hide() 方法。

I'm currently having some issues with my menu, its pretty basic as per the requirements.

ISSUE:

A user can click on the menu button once and open the sub menu, the user then clicks on the next button opening that sub menu, this is repeated as per the number of menu options. Once the user has clicked through them, if the user were to click another menu button once, it will not open, if the user clicks it a second time it will open. So after one run through the menu will only respond to two user clicks rather than a single one. This problem is occuring in other areas.

I suspect its something to do with my click handlers and event bubbling but my JQuery knowledge is self taught so I could do with a couple of pointers:

JQuery Code

$(document).ready(function(){
$('#menu span').hide();
$('#menu li').not('.sub_menu').toggle(function(){
    //$('#menu span').hide();
    $('.open').hide();
    //$(this).next().not('#mm_choice, .sub_menu').show();
    $(this).nextUntil('li').not('#mm_choice, .sub_menu').show().addClass('open');
    }, function(){
    //$(this).next().not('#mm_choice, .sub_menu').hide();
    //$(this).nextUntil('li').not('#mm_choice, .sub_menu').hide().removeClass('open');
    $('.open').removeClass('open').hide();
        });
});

HTML SOURCE CODE

<div id="menu" title="Main Menu">
<ul id="m_choices" class="main_menu">

<li id="mm_choice" title="Customer Segmentation Menu">Customer Segmentation</li>

    <span id="c_seg"><li class="sub_menu" title="Cust Seg Menu1">Cust Seg Menu1</li><li id="c_seg" class="sub_menu" title="Cust Seg Menu2">Cust Seg Menu2</li><li id="c_seg" class="sub_menu" title="Cust Seg Menu3">Cust Seg Menu3</li></span>

<li id="mm_choice" title="Industry Menu">Industry Selection</li>

    <span id="all_ind"><li class="sub_menu" title="View All Industries">View All</li></span>

    <span id="spec_ind"><li class="sub_menu" title="View information on a specific Industry">View Specific Industry</li></span>

<li id="mm_choice" title="Customer Satisfaction Menu">Customer Satisfaction</li>

    <span id="all_cs"><li class="sub_menu" title="View every industry's CS survey results">View All</li></span>

    <span id="spec_cs"><li class="sub_menu" title="View a specific industry's CS survey results">View Specific Industry</li></span>

<li id="mm_choice" title="Performance Measures Menu">Performance Measures</li>

    <span id="all_pm"><li class="sub_menu" title="Trend Menu">View All</li></span>

    <span id="spec_pm"><li class="sub_menu" title="Trend Menu">View Specific Industry</li></span>

<li id="mm_choice" title="Business Performance Menu">Business Performance</li>

    <span id="all_bp"><li class="sub_menu" title="Bus Perf Menu">View All</li></span>

    <span id="spec_bp"><li class="sub_menu" title="Bus Perf Menu">View Specific Industry</li></span>

<li id="mm_choice" title="Trend Analysis Menu">Trend Analysis Menu</li>

    <span id="all_trends"><li class="sub_menu" title="Trend Menu">View All</li></span>

    <span id="spec_trends"><li class="sub_menu" title="Trend Menu">View Specific Industry Trends</li></span>  
</ul>
</div>

The menu.php file (html mark up) is imported (using php include();) into the index.php file. The JQuery file is imported in declerations.php, imported into index.php in the header. Declerations.php contains all necessary JQuery libraries. Intention is to add UI animation at a later stage so the .show() and .hide() methods will be elaborated upon.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迎风吟唱 2024-11-01 04:01:31

我建议使用 .click() 方法,而不是使用菜单切换。单击可以让您更轻松地控制发生的事情。当您希望单击时有两种不同的状态时,Toggle() 非常有用,但对于复杂的菜单,它可能不是最好的方法。

此外,您还将辅助导航包装在跨度标签和列表项内的各个元素中。这是无效的 HTML 标记,会在某些浏览器中导致呈现问题。我建议将这些跨度更改为 ul

Instead of using a toggle for the menu I would suggest using the .click() method. The click would let you control what happens a little easier. Toggle() is great when you want to have two different states on a click but for a complex menu it may not be the best method.

Also you are wrapping your secondary navigation in span tags and the individual elements inside list-items. This is invalid HTML markup and will cause rendering issues in certain browsers. I would suggest changing those spans to ul's

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文