简体   繁体   English

jQuery .click()不会触发,但console.log显示输出

[英]jQuery .click() doesn't fire but console.log shows output

I'm having a problem in my code where I click a DOM-element using JavaScript. 我在使用JavaScript单击DOM元素的代码中遇到问题。 The click does not work and I am almost sure it is no dumb programming mistake (always dangerous to say). 单击不起作用,我几乎可以确定这不是愚蠢的编程错误(总是很危险地说)。

After deleting some DOM-elements I want my code to click an element and trigger its onclick event. 删除一些DOM元素后,我希望代码单击一个元素并触发其onclick事件。 However this doesn't work. 但是,这不起作用。 According to my code the event triggers but the event doesn't happen and the click event returns the jQuery object. 根据我的代码,事件会触发但事件不会发生,而click事件会返回jQuery对象。

HTML: HTML:

<div class="castor-tabs">
  <div class="castor-tab" data-path="../SiteBuilding/alertbox.js" data-saved="saved" data-editor="5475c897f1900editor">
    <span class="castor-filename">alertbox.js</span>
    <span class="castor-close"><i class="fa fa-fw fa-times"></i></span>
  </div>
  <div class="castor-tab" data-path="../SiteBuilding/index.php" data-saved="saved" data-editor="5475c89903e70editor">
    <span class="castor-filename">index.php</span>
    <span class="castor-close"><i class="fa fa-fw fa-times"></i></span>
  </div>
  <div class="castor-tab active" data-path="../SiteBuilding/makesite.php" data-saved="saved" data-editor="5475c8997ac77editor">
    <span class="castor-filename">makesite.php</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
                window.newTab = tab.next();
            } else if(tab.prev().length > 0) {
                // If element is to the left
                window.newTab = tab.prev();
            }
        } else {
            window.newTab = false;
        }

        var editor = tab.attr("data-editor");
        $("#" + editor).remove(); // textarea linked to CodeMirror
        $("#" + editor + "editor").remove(); // Huge CodeMirror-element
        tab.remove();

        if(window.newTab) {
            console.log("window.newTab.click()");
            console.log(window.newTab.click()); // Simulate click()
        }
    } else {
        // File isn't saved
    }
});

The onclick event: onclick事件:

$(".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();
});

I saved the element in the window object for a reason. 我将元素保存在window对象中是有原因的。 After the code runs and it skips the click-part I still have the DOM-element i want to click saved in the window object. 在代码运行并跳过单击部分之后,我仍然想将要单击的DOM元素保存在window对象中。 This means I can run 这意味着我可以跑步

console.log(window.newTab.click());

again. 再次。 Surprisingly this does click the element and this does activate the click-event. 令人惊讶的是,这确实单击了元素,并且确实激活了点击事件。 It also returns the DOM-element instead of the jQuery-object. 它还返回DOM元素而不是jQuery对象。

在此处输入图片说明

The image shows in the first two lines the failed click. 该图像在前两行显示失败的单击。 The third line is my manual input and the fourth line is the successful return value of the click(). 第三行是我的手动输入,第四行是click()的成功返回值。

I hope you can help me to solve this. 我希望你能帮助我解决这个问题。

UPDATE UPDATE

.trigger("click") unfortunately gives the same output.. .trigger(“ click”)不幸地提供了相同的输出。

在此处输入图片说明

UPDATE 2 更新2

To help you i made the website available on a subdomain. 为了帮助您,我在子域上提供了该网站。 I know many of you hate it if you have to go to a different page but I hope you'll forgive me because in my opinion this cant be solved through JSFiddle. 我知道许多人讨厌它,如果您必须转至另一个页面,但我希望您能原谅我,因为在我看来,这无法通过JSFiddle解决。

The link is http://castor.marknijboer.nl . 链接是http://castor.marknijboer.nl

After clicking some pages to open try closing them and you'll see what i mean. 单击某些页面以打开后,尝试关闭它们,您将明白我的意思。

除了模拟点击之外,为什么不直接将点击逻辑从点击函数中拉出来,而仅使用执行业务逻辑所需的参数调用javascript函数呢?

try adding return false; 尝试添加return false; at the end, when binding .castor-close click event 最后,当绑定.castor-close click事件时

$(".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
                window.newTab = tab.next();
            } else if(tab.prev().length > 0) {
                // If element is to the left
                window.newTab = tab.prev();
            }
        } else {
            window.newTab = false;
        }

        var editor = tab.attr("data-editor");
        $("#" + editor).remove(); // textarea linked to CodeMirror
        $("#" + editor + "editor").remove(); // Huge CodeMirror-element
        tab.remove();

        if(window.newTab) {
            console.log("window.newTab.click()");
            console.log(window.newTab.click()); // Simulate click()
        }
    } else {
        // File isn't saved
    }

    return false;
});

Your code is working but not able to trigger click event binded to castor-close because i tag is empty. 您的代码正常工作,但由于i标记为空,因此无法触发绑定到castor-close点击事件。 Put some text in it and check 在其中放一些文字并检查

<span class="castor-close"><i class="fa fa-fw fa-times">Click me</i></span>

DEMO DEMO

您的点击处理程序要求点击目标应具有Castor-close类,但是当您通过JavaScript调用click时,您单击的是父元素(.castor-tab),并且点击处理程序不响应。

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

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