【问题标题】:How to disable attribute menu item after it is clicked?单击后如何禁用属性菜单项?
【发布时间】:2017-07-07 19:21:40
【问题描述】:

我可以快速点击照片菜单 4 次,“$.get(...” 被发送到服务器 4 次。 我需要禁用此菜单(单击一次后),以便无法再次单击它。 环境:NodeJS、Express、Pug、Jquery 谢谢, 帕特

--------------DOM----------------------------------
// file = photos.pug
div#wrapper2
    div#altnav
      ul
        include includes/altnav.pug
        include includes/navjs.pug
---------------------------------------------------
// file = includes/altnav.pug
li: a.navigation(href='#', data-level='/images/cs/Photos') Photos
---------------------------------------------------
// file = includes/navjs.pug
script.
    $(function(){ // jQuery DOM ready
        $('.navigation').click(function () {
            var level = 'm' + $(this).data('level');        // Append an 'm' so I know this came from navjs.pug
            var url = '/navigation?level=' + escape(level); // Send this to the /router/photos.js
            $.get(url, function (data) { 
                $('body').html(data);           // data = new HTML page to be loaded by browser
            });
        });
    });

【问题讨论】:

    标签: jquery html node.js express


    【解决方案1】:
    $(function(){ // jQuery DOM ready
       $('.navigation').click(function () {
          $(this).css({
            "pointer-events": "none",
            "cursor": "default"
          });
              /* then your code */
       });
    });
    

    【讨论】:

    • 很高兴我能帮上忙 :)
    • 如何禁用所有菜单项,一旦其中一个被点击?
    • 如果他们是兄弟姐妹$(this).siblings().css(...)
    • 他们是兄弟姐妹,但是 $(this).siblings().css(...) 并没有禁用任何菜单,即使是我点击的那个。
    • 我会针对这种情况写一个新问题。
    【解决方案2】:

    我通常会为此添加一个标志到这样的单击元素

    $('.navigation').on('click',function(){
        if(!$(this).data('clicked')){
             $(this).data('clicked',true);
             // do rest of your staff
        }
    });
    

    或者如果你想禁用它,如果它是一个表单元素,你可以使用:

    $('.navigation').on('click',function(){ 
         // do rest of your staff
         $(this).prop( "disabled", true );
    });
    

    或者如果你想阻止指针交互使用 CSS。 pointer-events: none;你可以使用:

    $('.navigation').on('click',function(){ 
         // do rest of your staff
         $(this).css( "pointer-events", 'none' );
    });
    

    【讨论】:

    • 我不确定如何添加此标志。 rel=#selector 或 alt=#selector 或 rel=selector,或 alt=selector,或其他。
    • #selector 我的意思是你想在点击时绑定的元素。在你的情况下,你应该用$('.navigation') 覆盖它。我会更新答案以便更好地理解。
    猜你喜欢
    • 1970-01-01
    • 2021-06-20
    • 1970-01-01
    • 2021-06-24
    • 1970-01-01
    • 2013-03-30
    • 2012-11-15
    • 2017-12-11
    • 2012-08-30
    相关资源
    最近更新 更多