简体   繁体   English

如何使用jQuery暂时禁用导航链接

[英]How to temporarily disable a navigation link using jQuery

I'm still learning a bit on javascript/jquery and running into a bump. 我仍然在学习关于javascript / jquery的知识,并遇到了麻烦。 Every post I pull seems to over-complicate the process, unless it really does require all the extra code. 除非确实需要所有额外的代码,否则我发布的每篇文章似乎都会使该过程过于复杂。

Here's what I'm doing: 这是我在做什么:

  • Creating a vertical navigation menu with sliding menu's and static sub-menu's 使用滑动菜单和静态子菜单创建垂直导航菜单
  • Using HTML (5) layout with DL, DT and DD 将HTML(5)布局与DL,DT和DD一起使用
  • Nav menu is using minor CSS for styling 导航菜单使用次要CSS进行样式设置
  • Nav menu is using jQuery (1.8.3) 导航菜单正在使用jQuery(1.8.3)

I have everything working the way I want, but because I'm picky, I want to temporarily disable the link after a menu is expanded. 我可以按照自己的方式进行所有操作,但是由于我很挑剔,因此我想在菜单展开后暂时禁用该链接。 If I try to click the menu that is already expanded, it slides up and then back down. 如果我尝试单击已经展开的菜单,则它会先滑后滑。 What I wanted to do is make it so it just doesn't react to a click if it's already expanded. 我想做的就是使它如此,如果它已经扩展,它不会对单击做出反应。

HTML of Nav Menu: 导航菜单的HTML:

<dl class="nav2">
  <dt><a href="#">Thing1</a></dt>
    <dd>
        <ul>
            <li><a href="#">Test Def1</a></li>
            <li><a href="#">Test Def2</a><li>
            <li><a href="#">Test Def3</a></li>
        </ul>
    </dd>
  <dt><a href="#">Thing2</a></dt>
    <dd>
        <ul>
            <li><a href="#">Test Def4</a></li>
            <li><a href="#">Test Def5</a><li>
            <li><a href="#">Test Def6</a></li>
        </ul>
    </dd>
  <dt><a href="#">Thing3</a></dt>
    <dd>
        <ul>
            <li><a href="#">Test Def7</a></li>
            <li><a href="#">Test Def8</a><li>
            <li><a href="#">Test Def9</a></li>
        </ul>
    </dd>
</dl>

jQuery for Nav Menu: 导航菜单的jQuery:

$(document).ready(function() {
  $("dd:not(:first)").hide();
  $("dt a").click(function() {
    /* was thinking the added code would go here, but I could be wrong */
    $("dd:visible").slideUp("fast");
    $(this).parent().next().slideDown("fast");
    return false;
  });
});

I've tried a few things with bind and one, but due to my confusion with writing js/jquery, I'm not finding my trick. 我已经尝试过用bind和one进行一些操作,但是由于我对编写js / jquery感到困惑,所以找不到窍门。 Is there anyway possible to say something like: 无论如何,有没有可能说出类似以下内容:

$(this:active).unbind("click");

or 要么

if ($(this).active(function() {
  $(this).unbind("click");
} else {
  $(this).bind("click");
)};

I know I'm probably way off, but I'm trying. 我知道我可能已经走了,但我正在尝试。 Is there any way to change this into javascript/jQuery? 有什么办法可以将其更改为javascript / jQuery?

When the DT A is clicked -
  make THIS DT / DT A not clickable;
  When THIS DT / DT A is no longer expanded or visible -
  make THIS DT / DT A clickable;

Thanks for the peak. 感谢高峰。 Sorry if this was found somewhere. 抱歉,如果在某处找到它。 Each post I've ran into starts expanding this tiny change into several lines of code, whether attacking the CSS, longer then what seems to be needed javascript/jQuery or both. 我遇到的每篇文章都开始将这种微小的变化扩展为几行代码,无论是攻击CSS,还是需要更长的时间(似乎需要使用javascript / jQuery或同时使用这两者)。 I just really want to try to keep it contained and simple (if at all possible). 我只是真的想尝试使其保持简洁和简单(如果可能的话)。

You do not want to disable the click (by disabling the anchor, or unbinding the event). 您不想禁用单击(通过禁用锚或取消绑定事件)。 You want to not execute an action when the clicked element is currently selected. 当前不选择单击的元素时,您不想执行任何操作。 Do that like this: 这样做:

$(document).ready(function() {
    var active = $("dd:first");
    $("dd").not(active).hide();
    $("dt a").click(function() {
        var dd = $(this).parent().next();
        if (! dd.is(active)) {
            active.slideUp("fast");
            active = dd.slideDown("fast");
        }
        return false;
    });
});

You could add a temporary class to a link which has been clicked, and then remove it once whatever action the event triggered is complete. 您可以将临时类添加到已单击的链接,然后在事件触发的任何操作完成后将其删除。 Test for the class each time a link is click with hasClass, if it does, do nothing, if it doesn't, do something. 每次单击hasClass的链接时,都要测试该类,如果有,则什么都不做,如果没有,则做什么。 I have answered a similar question here: 我在这里回答了类似的问题:

Suppress jQuery event handling temporarily 暂时禁止jQuery事件处理

Here's how it would work with your code (I believe, though may need modification to suit your needs): 这是如何与您的代码一起工作的(我相信,尽管可能需要进行修改以满足您的需要):

$(document).ready(function() {
  $("dd:not(:first)").hide();
  $("dt a").click(function(e) {

    // Prevent link from doing default action
    e.preventDefault();

    if ($(this).hasClass('fired') == false) {

        // Add 'fired' class to disable link
        $(this).addClass('fired');

        // Do some stuff
        $("dd:visible").slideUp("fast");
        $(this).parent().next().slideDown("fast");

        // Remove 'fired' class to re-enable the link
        // You may need to do this in a call back function
        // If you are animating something
        $(this).removeClass('fired');

    }

    // Use preventDefault instead of this
    /*return false;*/
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM