简体   繁体   English

JavaScript跳过代码(因为JavaScript引擎忙吗?)

[英]JavaScript skips code ( because JavaScript engine is busy? )

I encountered a problem in my code when I try to execute some code after my JavaScript removes a lot of DOM-elements. 当我的JavaScript删除了很多DOM元素后尝试执行一些代码时,我的代码遇到了问题。

I am developing an webapplication named Castor. 我正在开发一个名为Castor的Web应用程序。 It is a code editor and it works pretty well. 它是一个代码编辑器,并且运行良好。 The problem is that i'm having an error after I closed an editor tab. 问题是关闭编辑器选项卡后出现错误。 I want the editor to select one tab after closing another. 我希望编辑器在关闭另一个选项卡之后选择一个选项卡。 Unfortunately this doesn't work. 不幸的是,这不起作用。 The code near the removing of the DOM-elements doesn't get executed. 删除DOM元素附近的代码不会执行。

HTML 的HTML

<div class="castor-tabs">
    <div class="castor-tab" data-path="../KeerpuntMobile/script/functions.php" data-saved="saved" data-editor="5475045011989editor">
        <span class="castor-filename">functions.php</span>
        <span class="castor-close"><i class="fa fa-fw fa-times"></i></span>
    </div>
    <div class="castor-tab" data-path="../KeerpuntMobile/script/jquery-1.10.2.min.js" data-saved="saved" data-editor="54750453e81a3editor">
        <span class="castor-filename">jquery-1.10.2.min.js</span>
        <span class="castor-close"><i class="fa fa-fw fa-times"></i></span>
    </div>
    <div class="castor-tab active" data-path="../KeerpuntMobile/script/functions.js" data-saved="saved" data-editor="54750457a94f6editor">
        <span class="castor-filename">functions.js</span>
        <span class="castor-close"><i class="fa fa-fw fa-times"></i></span>
    </div>
</div>

JavaScript: JavaScript:

$(".castor-tabs").on("click", ".castor-close", function() {
        var tab = $(this).parent();
        if(tab.attr("data-saved") == "saved") {
            // File is saved

            if($(".castor-tab").length > 1) {
                // 1 element is 'tab' the other is a second tab

                if(tab.next().length > 0) {
                    // If element is to the right
                    tab.next().click();
                    console.log(tab.next().attr("data-editor"));

                } else if(tab.prev().length > 0) {

                    // If element is to the left
                    tab.prev().click();
                    console.log(tab.prev().attr("data-editor"));

                }
            }

            var editor = tab.attr("data-editor");
            $("#" + editor).remove(); // textarea linked to CodeMirror
            $("#" + editor + "editor").remove(); // Huge CodeMirror-element
            tab.remove();
        } else {
            // File isn't saved
        }
    });

$(".castor-tabs").on("click", ".castor-tab", function() {
    $(".castor-tab.active").removeClass("active");
    $(this).addClass("active");
    var editor = $(this).attr("data-editor");
    $(".CodeMirror").hide();
    $("#" + editor).show();
});

标签

If I click on the cross of the middle tab the editor disappears, the tab disappears, I get an output in my console, but none of the other tabs get clicked. 如果单击中间选项卡的十字,则编辑器消失,该选项卡消失,控制台中显示输出,但其他选项卡均未单击。

The output of the console.log is "54750457a94f6editor". console.log的输出是“ 54750457a94f6editor”。

This shows that tab.next() represents the the tab with the filename 'functions.js' which is the right one. 这表明tab.next()代表带有正确文件名'functions.js'的标签页。

I've replaced the $(element).click() with $(element).trigger("click") and it didn't help. 我已经用$(element).trigger(“ click”)替换了$(element).click(),但它没有帮助。 I even pasted the whole click-event code in this if-statement but nothing seems to work. 我什至在此if语句中粘贴了整个点击事件代码,但似乎无济于事。 I hope you can help me with a solution. 希望您能为我提供解决方案。

See http://jsfiddle.net/vyL49LLk/ with awesome css. 参见http://jsfiddle.net/vyL49LLk/带有超赞的CSS。 You just need to add a space to your selector, change 您只需要在选择器中添加一个空格,然后更改

$(".castor-tab.active").removeClass("active");

to this: 对此:

$(".castor-tab .active").removeClass("active");

$(".castor-tab.active") is looking for elements which have both castor-tab and active classes. $(".castor-tab.active")正在寻找同时具有castor-tabactive类的元素。 $(".castor-tab .active") will look for elements which has class active and are descendants of castor-tab . $(".castor-tab .active")将查找具有active类并且是castor-tab后代的元素。

how about changing this line: 如何更改此行:

 tab.next().click();

to this line: 到这行:

 tab.next().children(':eq(0)').click(); // credit to Felix Kling for his comment

it seems like tab is the PARENT of the element with the click listener, so activating a click on the PARENT has no effect at all. tab似乎是具有单击侦听器的元素的PARENT ,因此激活对PARENT的单击完全无效。 i assume that the desirable div is the first child of that parent 我假设理想的div是那个父母的第一个孩子

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

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