onclick on on other里面的元素也是onclick使用的

时间:2014-09-29 16:32:37

标签: javascript jquery html css

这里有这样的问题,但我尝试了解决方案,在我的情况下,这不起作用。 我有一个通过单击打开的表单,然后必须通过单击此表单中的x来关闭它。 This variant不适合,因为Cookie在关闭表单后不会设置为on。 如果我打开将其设置为close click这也不起作用,因为点击x会调用所有表单的点击事件。 如果我喜欢solution, I find in stackoverflow,表单根本不会打开。 这是我的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:

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

那么如何点击交叉忽略主窗体?

1 个答案:

答案 0 :(得分:1)

这里有两个选项,停止在onclick a#closeChat中传播事件(注意我还在处理程序中添加了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/

OR

检查target处理程序上event的{​​{1}}属性:

div#techSupport

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

最佳解决方案取决于每个用例。

相关问题