0

嗨,我有一个网站,可以在容器内动态加载页面。我有一个这样的导航菜单:

<div class="menu">
    <p align="left" id="home" class="titolo_menu">home</p>
    <p align="left" id="azienda" class="titolo_menu azienda"><br>azienda</p>
    <p align="left" id="staff" class="titolo_sotto_menu azienda">staff</p>
    <p align="left" id="risorseumane" class="titolo_sotto_menu azienda">risorse umane</p>
</div>

在我的 jQuery 中,我添加了这一行:

$(document).ready(function(){
    $('.titolo_menu').bind('click',function(){
            $('.titolo_menu').unbind('click');
             //more code...
     });
  });

我希望当用户单击具有类“titolo_menu”的 ap 时禁用事件单击但不起作用,我可以单击越来越多的时间。我该如何解决?

4

4 回答 4

2

虽然我无法弄清楚您在应用程序中真正想要什么,但请考虑使用data-*属性来与 DOM 本身内部的某些状态相同,例如:

$(document).ready(function(){
    $('.titolo_menu').on('click',function(){

        // let's create a reference for current element
        $p = $(this);

        // if clicked element had data-disabled attribute set, return
        if($p.data('disabled'))
            return;

        // disable element;
        $p.data('disabled', true);

        // do your stuff here, e.g.
        $.get('dapage.html', function(data){

            // do your stuff on data retrieving
            $(something).html(data);

            // re-enable element
            $p.data('disabled', false);

        });

   });
});

这样您就可以控制事件,而不会弄乱解除绑定。

于 2012-10-31T15:34:37.833 回答
0

你可以试试这个......

$(document).ready(function(){
var return_flag = true;
    $('.titolo_menu').bind('click',function(){
        if(return_flag){
             //more code...
        }
    return_flag = false;
     });
  });
于 2012-10-31T15:06:00.013 回答
0

只需更改班级名称

$('.titolo_menu').bind('click',function(){
           $(this).removeClass('.titolo_menu');
             //more code...
     });

或者,如果您想禁用所有元素:

$('.titolo_menu').bind('click',function(){
            $('.titolo_menu').removeClass('.titolo_menu');
             //more code...
     });

如果您使用 'live' insted of 'bind' 则不起作用

//编辑抱歉,它不起作用。只需使用现场:

$('.titolo_menu').live('click', function(){
  $('.titolo_menu').removeClass('.titolo_menu');
}); 

但是,如果您只是想防止在加载页面时多次单击,请使用

.attr('disabled', 'disabled');
于 2012-10-31T15:13:51.737 回答
0

首先,我想建议您在创建菜单时使用未排序的列表并使用<a>标签来链接页面。

例子:

<ul id="menu">
    <li id="home" class="titolo_menu"><a href="#home">home</a></li>
    <li id="azienda" class="titolo_menu azienda"><a href="#azienda">azienda</a></li>
    <li id="staff" class="titolo_sotto_menu azienda"><a href="#staff">staff</a></li>
    <li id="risorseumane" class="titolo_sotto_menu azienda"><a href="#risorseumane">risorse umane</a></li>
</ul>

在 jQuery 中,您可以执行以下操作:

$(function() {

    $("#menu a").click(function(e) {
        var clickedElement = $(this);
        var clickedElementText = clickedElement.text();

        // Check if current link is not yet active
        if (!$(clickedElement).hasClass("active")) {

            if ($(".active").length > 0) {
                // Deactivate current active link
                var active = $(".active ");
                var activeText = $(active).text();

                $(".active").replaceWith("<a href = '#" + active.parent().attr("id") + "' > " + activeText + " </a>");
            }
            // Change the clicked link to be activated
            $(clickedElement).replaceWith("<span class='active'>" + clickedElementText + "</span>");

            // Load your page or whatever here :)

            // Prevent normal link behavior
            e.preventDefault();
        }
    });
});
于 2012-10-31T15:51:41.047 回答