下拉框一直向上滚动

Drop Down Box keeps scrolling up

本文关键字:滚动 一直      更新时间:2023-09-26

我从之前的问题中实现了这个功能,但由于某些原因,在Firefox和IE上,下拉框会自动滚动。我不知道为什么!

只需点击News Feed,当框下降时,它会自动下降。它应该是下拉的,如果我再次点击newfeed,它应该是上拉的。但它不会那样做,它只是弹回来。

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

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

更改您的#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()将阻止事件冒泡。

Docs for event.stopPropagation(): http://api.jquery.com/event.stoppropagation/