event.preventDefault()的问题无效

时间:2012-10-18 06:42:37

标签: javascript jquery

寻找有关正确使用preventDefault()的一些建议 - 代码如下。简而言之,我不希望a.dropdownTrigger单击事件跳转到锚点。我想插入event.preventDefault()作为该函数的第一行就可以了,但显然不是。

认为它可能与绑定hashchange事件有关,我正用它来监视url的更改(单击a.dropdownTrigger元素更新哈希位置,它在hashchange上调用侦听器 - 这样做方式使我能够捕获到dropdownTrigger的入站链接,并维护网址历史记录。)

我出错的任何想法?

    // check inbound anchor links against dropdown ids
    if (hash != "" && dropdowns.length != 0 && $.inArray(hash, dropdowns)) {
        OpenDropdownForHash(hash);
    }

    // listen for hashchange once page loads (handles on-page links to dropdown content)
    $(window).bind('hashchange', function () {
        hash = window.location.hash;
        if (dropdowns.length != 0 && $.inArray(hash, dropdowns)) {
            OpenDropdownForHash(hash);
        }
    });

    // open the targeted dropdown - var incoming is a bool, differentiate between inbound links and on-page clicks
    function OpenDropdownForHash(x) {

        $(x).next('.dropdown').toggleClass('open').slideToggle(200);

        if ($(x).next('.dropdown').hasClass('open')) { //can this live in the callback above? 
            $(x).parent().css("-webkit-transition", "all 0.8s ease")
                .css("backgroundColor", "white")
                .css("-moz-transition", "all 0.8s ease")
                .css("-o-transition", "all 0.8s ease")
                .css("-ms-transition", "all 0.8s ease")

                .css("backgroundColor", '#eeeeee').delay(600).queue(function () {
                    $(this).css("backgroundColor", "white");
                    $(this).dequeue(); //Prevents holding color with no fadeOut on second click.
                });
        }
    }

    // finally, the basic click handler for dropdowns - update the hash (to allow history), which triggers previously bound hashchange event
    $('a.dropdownTrigger').bind('click',function (e) {
        e.preventDefault();
        if ("#" + $(this).attr('id') == location.hash) { // clicking an open dropdown link doesn't trigger the hashchange event, so we check manually
            OpenDropdownForHash("#" + $(this).attr('id'));
        }
        else { // clicking a closed dropdown does call hashchange, so the OpenDropdownForHash function is called by the listener
            location.hash = $(this).attr('id');
        }
    });

更新:解决了这个问题,重新设计了点击处理程序并简化了hashchange监听器:

    if (dropdowns.length != 0) {
        // the basic click handler for dropdowns - update the hash (to allow history), which triggers previously bound hashchange event
        $('#mainContentContainer a').bind('click', function (e) {
            var target = $(this).attr('href') != null ? $(this).attr('href') : "#" + $(this).attr('id');
            var offset = window.pageYOffset;
            if ($.inArray(target, dropdowns) && location.hash != target) {                    
                location.hash = target;
                window.scrollTo(0, offset);
            }
            else if ($.inArray(target, dropdowns) && location.hash == target) {
                OpenDropdownForHash(location.hash, $(this).hasClass('dropdownTrigger'));
                window.scrollTo(0, offset);
            }
        });
    }

    $(window).bind('hashchange', function(e) {
        OpenDropdownForHash(location.hash, $(this).hasClass('dropdownTrigger'));  
    });

1 个答案:

答案 0 :(得分:0)

如果点击链接更新哈希,则e.preventDefault();无效。检查链接是否真的受到限制。

好像他们不是。尝试将e.preventDefault();替换为alert('I am bound!!!');,看看点击链接后会发生什么。

您是否已准备好文档中的代码?

编辑:如果我理解正确,你的锚点击处理程序是多余的,因为点击链接会更新哈希本身。