简体   繁体   English

Selectivizr导致选择下拉菜单需要在IE8及以下版本中打开两次

[英]Selectivizr causes select dropdown to require two clicks to open in IE8 & below

I recently implemented Selectivizr on an internal company website as we need to support IE7/8. 我最近在内部公司网站上实施了Selectivizr,因为我们需要支持IE7 / 8。 Unfortunately, our site does a lot of dynamic content loading via jQuery/AJAX. 不幸的是,我们的网站通过jQuery / AJAX进行了大量的动态内容加载。

To solve this problem, I overloaded the jQuery ready function to reload Selectivizr after it performs whatever task it was set out to. 为了解决这个问题,我重载了jQuery ready函数,在它执行任何设置的任务后重新加载Selectivizr。 My code looks like this: 我的代码看起来像这样:

$(function () {
    if ($('body.ie8, body.ie7, body.comp-view8, body.comp-view7').length > 0) {
        (function () {
            var original = jQuery.fn.ready;
            var getSelectivizr;
            jQuery.fn.ready = function () {
                // Execute the original method.
                original.apply(this, arguments);

                clearTimeout(getSelectivizr);
                getSelectivizr = setTimeout(function () {
                    $.getScript(selectivizr);
                }, 50);
            }

        })();
    }
});

Simple enough. 很简单。 However, a teammate recently discovered a bug that seems to be related. 然而,一位队友最近发现了一个似乎与之相关的错误。 In IE8/7 any select dropdown that is dynamically loaded into the page (I'm not sure if static dropdowns are effected as well as none of these pages have them), requires two clicks to open it. 在IE8 / 7中,动态加载到页面中的任何选择下拉列表(我不确定静态下拉列表是否受影响以及这些页面都没有这些下拉列表),需要两次单击才能打开它。

To be more specific, in IE8/7, the first click seems to "focus" on the dropdown, while the second opens it. 更具体地说,在IE8 / 7中,第一次点击似乎“关注”下拉列表,而第二次点击则打开它。 It Compatibility View, it actually opens for a split-second and then closes. 它的兼容性视图,它实际上打开了一瞬间然后关闭。 The second click opens it just fine (as long as you remain focused on the dropdown). 第二次点击打开它就好了(只要你继续关注下拉列表)。

I had assumed it might be an issue with what Selectivizr was doing, as it wasn't really designed to work with dynamically loaded content, but after a little bit of debugging, it seems that it's the setTimeout that is causing this strange behavior. 我认为它可能是Selectivizr正在做的事情的一个问题,因为它并不是真的设计用于动态加载的内容,但经过一些调试后,似乎是导致这种奇怪行为的setTimeout。

I'm at a complete loss how to fix this without removing my Selectivizr implementation . 我完全失去了如何在不删除我的Selectivizr实现的情况下解决这个问题

It is probably worth noting that the setTimeout is necessary to prevent the browser from attempting to load Selectivizr multiple times if different AJAX calls are made as this can cause serious performance issues within the browser. 值得注意的是,如果进行不同的AJAX调用,则setTimeout是必要的,以防止浏览器多次尝试加载Selectivizr,因为这可能会在浏览器中导致严重的性能问题。

Note: this question does not accurately reflect the issue stated in the title, so I updated it to provide better searching! 注意:这个问题没有准确反映标题中陈述的问题,所以我更新了它以提供更好的搜索! After coming back to this problem a few weeks later, I recognized that my initial debugging had led me down the wrong path. 几周后回到这个问题后,我意识到我的初步调试让我走上了错误的道路。 Sorry folks, but I've provided an answer to this which I hope helps!:) 对不起,但我已经提供了一个答案,我希望对此有所帮助!:)

So, I've finally had a chance to get back to this bug and it seems the solution was staring me in the face all along, I had just totally missed it due to botching up my initial debugging. 所以,我终于有机会回到这个错误了,似乎解决方案一直盯着我,我刚刚完全错过了它,因为我的初始调试很糟糕。

It turns out it was a selectivizr issue all along. 事实证明这一直是一个选择性问题。 Unfortunately, making any sort of change dynamically (JS) to a select box in IE8 and below causes it to be redrawn, which forces it to close (or never open, depending on the version/mode). 不幸的是,对IE8及以下的选择框动态地进行任何类型的更改(JS)会导致重新绘制,这会强制它关闭(或永远不会打开,具体取决于版本/模式)。 The way selectivizr works is that it discretely adds a class to elements, such as "slvzr-focus", using JS to mimic the behavior of pseudo-classes; selectivizr的工作方式是它使用JS来模仿伪类的行为,从而离散地为元素添加一个类,例如“slvzr-focus”。 in this case ":focus". 在这种情况下“:焦点”。

As such, it made sense to simply restrict selectivizr from applying this kind of patch to select boxes on focus. 因此,简单地限制选择器应用这种补丁来选择焦点框是有意义的。 My solution is as follows, though it may not be for everyone (alternatively, you can simply ensure that no ":focus" selectors exist in your CSS, which will cause selectivizr to never fire the event): 我的解决方案如下,虽然它可能并不适合所有人(或者,您可以简单地确保CSS中不存在“:focus”选择器,这将导致选择器永远不会触发事件):

1) Find the following line in selectivizr.js: 1)在selectivizr.js中找到以下行:

if (!hasPatch(elm, patch)) {

    if (patch.applyClass && (patch.applyClass === true || patch.applyClass(elm) === true)) {

        cssClasses = toggleClass(cssClasses, patch.className, true);
    }

}

2) Wrap it with the following if statement: 2)用以下if语句包装它:

if (!(elm.tagName == 'SELECT' && patch.className == 'slvzr-focus')) {
}

3) The block should look like this when you're done: 3)完成后 ,块应如下所示:

if (!(elm.tagName == 'SELECT' && patch.className == 'slvzr-focus')) {
    if (!hasPatch(elm, patch)) {

        if (patch.applyClass && (patch.applyClass === true || patch.applyClass(elm) === true)) {

            cssClasses = toggleClass(cssClasses, patch.className, true);
        }
    }
}

Hope that helps someone out there. 希望能帮到那里的人。

Thanks S/O! 谢谢S / O!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM