jQuery 使用垂直滚动条和 mousedown - FireFox 有效,IE 有问题

发布于 2024-10-18 03:13:43 字数 490 浏览 9 评论 0原文

我有一个用 jQuery 编写的菜单,当事件触发时会弹出。它的侧面有一个垂直滚动条。滚动在 FireFox 中工作正常,但在 IE(8 或 9)中不行。我想这与 mousedown 事件有关,需要从菜单中选择一个项目。

    .mousedown(function (event) {
        var li = get_element_from_event(event, "li");
        if(li){
            return false;
        }

对于 IE,当用鼠标单击滚动条时,菜单会再次折叠(就像用户进行了选择一样)。

这是 IE 中的常见问题(即滚动条问题)吗?

我认为这也与以下滚动条代码片段有关

$(".token-input-list")[0].scrollTop = $(".token-input-list")[0].scrollHeight;

I have a menu scripted with jQuery, which pops out when triggered by an event. It has a vertical scroll bar on the side. The scrolling works fine in FireFox, but not in IE (8 or 9). I guess it has something to do with the mousedown event, which is needed to select an item from the menu.

    .mousedown(function (event) {
        var li = get_element_from_event(event, "li");
        if(li){
            return false;
        }

In case of IE, when clicking with the mouse on the scrollbar, the menu collapses again (as if the user made a selection).

Is this a common problem in IE (i.e. issues with the scrollbar)?

I think it also has to do with the following snippet of scrollbar code

$(".token-input-list")[0].scrollTop = $(".token-input-list")[0].scrollHeight;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

み青杉依旧 2024-10-25 03:13:43

根据 get_element_from_event() 的作用以及您使用 mousedown 执行的其他操作,IE 可能会正确运行 - 如果事件目标不是元素,它会据说 li 返回 false,因此 if(li) 会失败,因此 mousedown 不会返回 false,但继续前进。

至于修复它,您需要找出 IE 为 li 返回的内容(通过警报或控制台),并在 get_element_from_event() 中对其进行测试> 或在新的变量/函数中。

Depending on what get_element_from_event() does, and what else you're doing with mousedown, IE may be behaving correctly--if the event target is not an element, it will supposedly return false for li, and so it will fail if(li) and therefore not return false for mousedown, but keep on chugging along.

As far as fixing it goes, you'll want to find out what IE is returning for li (via an alert or the console) and either test for it in get_element_from_event() or in a new var/function.

你是年少的欢喜 2024-10-25 03:13:43

如果您的 .mousedown 事件附加到滚动条所在的同一元素,请将您的 .mousedown 事件移动到具有滚动条的 div 内的新 div。

If your .mousedown event is attached to the same element the scrollbar is on, move your .mousedown event to a new div inside the div that has the scrollbar.

你是年少的欢喜 2024-10-25 03:13:43

感谢您的输入 - 我发现滚动条问题上有很多错误,因此我通过包含在这里找到的一个不错的列表脚本来解决它: http://rascarlito.free.fr/hoverscroll/。这对 IE 来说是个窍门,对我来说也是如此;)

Thanks for your input - I found quite a bit of buggyness on the scrollbar issue, so I solved it by inlcuding a nice list script found here: http://rascarlito.free.fr/hoverscroll/. Dit the trick for IE, and thus for me ;)

我要还你自由 2024-10-25 03:13:43

问题是使用全局window.event对象,而不是jQuery的事件对象。 window.event 仅适用于某些浏览器,并且它不是 W3C 标准。

jQuery 规范化了事件对象,因此它在所有浏览器中都是相同的。事件处理程序将该 jQuery 事件对象作为参数传递。使用那个。

$(".class_name").mousedown(function (e) {

  switch (e.which) {
    case 1: //leftclick
        //...
        break;
    case 3: //rightclick
        //...
        break;
  }
});

The problem is that using the global window.event object, and not jQuery's event object. window.event only works in some browsers, and it is not a W3C standard.

jQuery normalizes the event object so it's the same in all browsers. The event handler is passed that jQuery event object as a parameter. using that.

$(".class_name").mousedown(function (e) {

  switch (e.which) {
    case 1: //leftclick
        //...
        break;
    case 3: //rightclick
        //...
        break;
  }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文