下拉框不断向上滚动

时间:2011-12-11 01:19:28

标签: javascript jquery html css

我从之前的问题实现了这个到我的网站但由于某种原因在Firefox和IE上,下拉框自动向上滚动。我无法弄明白为什么!

只需点击“新闻Feed”,当该框下拉时,它会自动降低。它应该下降,如果我再次或在外面点击newfeed,它应该放弃。但它没有这样做,它只是重新振作起来。

我正在使用JavaScript。这是怎么回事?

$('#bottom').click(function() {
    $('#content').slideDown();
});

$(document).click(function(e) {
    if (e.target.id !='bottom') {
        $('#content').slideUp();
    }
});

1 个答案:

答案 0 :(得分:4)

稍微更改您的#bottom事件处理程序,以防止click事件一直冒出document

//it is important to declare the `event` variable as a parameter of this anonymous function so it can be accessed inside the function
$('#bottom').click(function(event) {
    event.stopPropagation();
    $('#content').slideDown();
});

您的代码发生的事情是#bottom元素的事件处理程序正在被触发,然后在document事件起泡后点击click的事件处理程序DOM。 event.stopPropagation()将阻止事件冒泡。

event.stopPropagation()的文档:http://api.jquery.com/event.stoppropagation/

相关问题