在点击事件上添加防止默认值?

时间:2016-02-12 10:23:58

标签: javascript jquery

我有一个点击事件,说明这些元素是否没有类运行功能......

$(document).on('click', '.arrow:not(".disabled")', altSaws);

这很好用,但是我的e.preventDefault在altSaws函数内,所以当元素禁用了类时,点击后用户就会被带到页面顶部,有没有其他方法可以做到这一点?

希望这有道理吗?

2 个答案:

答案 0 :(得分:1)

你的函数altSaws是问题中缺少的东西,但它应该像这样开始:

function altSaws(e) {
    e.preventDefault();
    ...     
}

答案 1 :(得分:1)

你可以有2个解决方案

将默认阻止调用移至单独的处理程序

#include <string>
#include <iostream>

class NameErrorException : public std::exception
{
    std::string m_what;
public:
    NameErrorException(std::string name)
        :m_what("NameErrorException: name \'" + name + "\' is not defined")
    {
    }

    const char* what() const throw() override
    {
        return m_what.c_str();
    }
};


int main() 
{
    try
    {
        throw NameErrorException("TEST");
    }
    catch (std::exception& ex)
    {
        std::cout << ex.what() << std::endl;
    }
}

或处理$(document).on('click', '.arrow:not(".disabled")', altSaws); function altSaws() { //no need to prevent default here } $(document).on('click', '.arrow', function(e) { e.preventDefault(); });

中的disabled条件
altSaws
相关问题