添加CSS元素:Jquery仅在Chrome开发人员工具打开时有效

时间:2017-07-31 02:49:58

标签: jquery google-chrome twitter-bootstrap-3

此问题与this post类似,但我只是通过添加CSS类来简单突出显示菜单项之一。

基本上,我正在尝试遍历#nav-accordion div下的菜单项列表。当IF条件为真时,我会将active类附加到li元素。

这是我的代码

$("#nav-accordion li a").each(function () {
    if (true) {
        $(this).closest("li").addClass("active");
        // Expand the parent menu
        $(this).closest("li").closest("ul").show();
        // Highlight the parent menu item

        $(this).closest("li").parent().parent().find("a").addClass("active");

       // Exit loop when one of the menu item is highlighted
       return;
    }
}

当我在Chrome中未打开开发者工具的情况下加载页面时,突出显示不起作用;但是当我打开开发人员工具并加载页面时,突出显示的工作就像魔术一样。现在我不知道这个奇怪的魔法是如何工作的。

This post描述我应该删除代码中的任何console.log,我这样做了。但同样的问题仍然存在。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您可以在jQuery $( window ).load()事件中尝试此操作。如果仍然得到相同的结果,请尝试使用jQuery.when(延迟)或尝试使用setTimeout选项。我有类似的问题,我用jQuery.when选项对它进行了排序。

相关问题