简体   繁体   English

onclick也在onclick所使用的其他元素之内

[英]onclick on element that is inside other that is used by onclick too

There is such question here, but I tried solution and in my case this doesn't work. 这里有这样的问题,但是我尝试了解决方案,但就我而言,这不起作用。 I have a form that opens by click on it and then it must be closed by clicking x that is inside this form. 我有一个通过单击打开的表单,然后必须通过单击此表单内的x来关闭它。 This variant doesn't fit, because cookie doesn't sets to on after closing form. 此变体形式不合适,因为关闭表单后cookie不会设置为on。 If I open set it on close click This doesn't works too because click on x calls click event of all form. 如果我打开将其设置为“ 关闭”,则单击将不会起作用,因为单击x会调用所有形式的click事件。 If I do like in solution, I find in stackoverflow the form doesn't opens at all. 如果在解决方案中喜欢,我会在stackoverflow中发现该表单根本没有打开。 Here is my js code: 这是我的js代码:

$(document).ready(function () {
    if ($.cookie('techSupport') == null) {
        $.cookie('techSupport', 'off');
    } else if ($.cookie('techSupport') == 'on') {
        $.cookie('techSupport', 'off');
    };
}).on("click", "div#techSupport", function (event) {
    if ($.cookie("techSupport") == 'off') {
        $.cookie("techSupport", "on");
        var res = "<nobr><div>Technical support<a href='#' id='closeChat'>x</a></div></nobr>\n" +
            "<div id='chatBox'>What's your probloem?</div>\n" +
            "<textarea id='messageBox'></textarea>\n" +
            "<button id='sendToSupport'>Send</button>";
        $(this).css("width", "200px");
        $(this).css("height", "400px");
        $(this).html(res);
    }
}).on("click", "div#techSupport,a#closeChat", function () {
    var res = "<div id='techSupport'>techsupport</div>";
    $("div#techSupport").html(res);
    $("div#techSupport").css("width", "100px");
    $("div#techSupport").css("height", "10px");
    console.log($("div#techSupport").html());
    $.cookie("techSupport","off");
    event.stopPropagation();
    //window.location.reload();
});

HTML: HTML:

<div id="techSupport">techsupport</div>

So how can I click on cross ignoring the main form? 那么如何单击交叉忽略主窗体?

You have two options here, stop the propagation of the event in onclick of a#closeChat (note I also added the e parameter at your handler): 您在这里有两个选择,在a#closeChat onclick中停止事件的传播(注意,我还在处理程序中添加了e参数):

.on("click", "a#closeChat", function (e) {
    var res = "<div id='techSupport'>techsupport</div>";
    $("div#techSupport").html(res);
    $("div#techSupport").css("width", "100px");
    $("div#techSupport").css("height", "10px");
    console.log($("div#techSupport").html());
    $.cookie("techSupport","off");
    e.stopPropagation();
    //window.location.reload();
});

http://jsfiddle.net/d6fpfzsu/10/ http://jsfiddle.net/d6fpfzsu/10/

OR 要么

Inspect the target property of the event on the div#techSupport handler: div#techSupport处理程序上检查eventtarget属性:

if ($.cookie("techSupport") == 'off' && event.target.id != 'closeChat') {
    $.cookie("techSupport", "on");
    var res = "<nobr><div>Technical support<a href='#' id='closeChat'>x</a></div></nobr>\n" +
        "<div id='chatBox'>What's your probloem?</div>\n" +
        "<textarea id='messageBox'></textarea>\n" +
        "<button id='sendToSupport'>Send</button>";
    $(this).css("width", "200px");
    $(this).css("height", "400px");
    $(this).html(res);
}

http://jsfiddle.net/d6fpfzsu/11/ http://jsfiddle.net/d6fpfzsu/11/

The best solution will depend on each use case. 最佳解决方案将取决于每个用例。

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

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