TypeError: e is undefined - javascript

TypeError: e is undefined - javascript

本文关键字:javascript undefined is TypeError      更新时间:2023-09-26

所有其他浏览器都能正常工作。然而,当firefox尝试执行以下代码时:

if (!e) var e = window.event;
e.cancelBubble = true;
if (e.stopPropagation) e.stopPropagation();

它崩溃,控制台显示以下错误:TypeError:e是未定义的


编辑1:

function clickInactiveTab() {
    $(this).attr({class: "activeTab"});
    $(".inactiveTab").hide();
}
function clickX() {
    $(this).parent().attr({class: "inactiveTab"});
    $(".inactiveTab").show();
    if (!e) var e = window.event;
    e.cancelBubble = true;
    if (e.stopPropagation) e.stopPropagation();
}

它所做的是,在单击时更改div的样式,并从类中隐藏所有其他div。当有人点击div内部的x时,它应该将样式改回并显示隐藏的div。

e未定义,因此这将是错误

function clickX(e) {  //e needs to be in the arguments as long as the event is attached properly, this will work.
    $(this).parent().attr({class: "inactiveTab"});
    $(".inactiveTab").show();
    e = e || window.event;
    e.cancelBubble = true;
    if (e.stopPropagation) e.stopPropagation();
}

如果使用jQuery附加事件,则没有理由检查事件或stopPropagation。