event.preventDefault()在firefox中不起作用

时间:2016-03-01 13:05:58

标签: javascript jquery html firefox

我想使用函数blow来防止默认操作,例如" a href",但它只能在firefox中工作。

function alertPopup(html) {
    event.preventDefault();
    // ...
}

然后我添加了变量" event"进入这个功能,作为打击,但也失败了; firefox控制台显示错误" ReferenceError:事件未定义"。

function alertPopup(html) {

    function stepstop(event){
        console.log("tttttt");
        event.stopPropagation();
    };
    stepstop(event);
            // ...
}

<a href="#" onclick="alertPopup("hello");">XXXXX</a>

那么如何防止此功能中的默认操作?不使用&#34;返回false&#34; ...谢谢!

2 个答案:

答案 0 :(得分:1)

尝试在Javascript中添加一个事件监听器,而不是在锚标签上使用onclick属性,这样就可以阻止对实际点击事件的默认。

使用JQuery,它看起来像这样:

$('a').on('click', function(e){
    e.preventDefault();
    ......
});

答案 1 :(得分:0)

传递事件

function alertPopup(event, html) {
    event.preventDefault();
    console.log(html);
}
<a href="#" onclick="alertPopup(event, 'hello');">XXXXX</a>

更好的方法是使用addEventListener()附加事件而不使用内联事件。

相关问题