我在之前的一个问题中将此实现到我的网站,但出于某种原因,在 Firefox 和 IE 上,下拉框会自行向上滚动。我不明白为什么!
只需单击动态消息,当框下降时,它会自动下降。它应该会下降,如果我再次或在外部单击 newfeed,它应该会下降。但它不会那样做,它只是弹回来。
我正在使用 JavaScript。这是怎么回事?
$('#bottom').click(function() {
$('#content').slideDown();
});
$(document).click(function(e) {
if (e.target.id !='bottom') {
$('#content').slideUp();
}
});
最佳答案
稍微更改一下您的#bottom
事件处理程序,以防止click
事件一直冒泡到文档
:
//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/
关于javascript - 下拉框一直向上滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8461231/