在点击时隐藏附加到div的元素?

时间:2014-12-31 08:44:09

标签: jquery

我正在使用以下内容在网页上添加一些包含div的div

var strScript = '<div class="' + cssClass + '" > ' + message + '</div>';
var notifyElement = $(strScript);

现在,当用户点击 OnClick

时,我想将此附加的div设置为消失

我可以做这样的事吗

notifyElement.OnClick(function .......

我无法通过传递id来选择元素,因此在一个页面中会有多个此类型的div。

2 个答案:

答案 0 :(得分:2)

是的,只需使用click功能(或on):

notifyElement.click(function() {
    notifyElement.hide();
});

notifyElement.on("click", function() {
    notifyElement.hide();
});

或者您将来可能会将notifyElement变量用于其他内容:

notifyElement.on("click", function() {
    $(this).hide();
});

答案 1 :(得分:1)

是的:

notifyElement.click(function() {
    $(this).hide();
});

或在创建时执行:

$(notifyElement, {
    "click": function() {
        $(this).hide()   
    }
});
相关问题