按类名jquery onclick - 如何

时间:2011-04-03 12:06:54

标签: javascript jquery

我有:<a class='category' href='view.php?category=1'>category</a>

我这样做:$("a.category").click(function(){alert("dsafdfad")})

没有任何反应?

开始( “view.php?”);     帖( “view.php?”);

function start(link){
    $("#country ul").load(link+"mode=start",function(){
    $("#country a").click(function(){
        var link=encodeURI($(this).attr("href"));
        category(link+"&");
        posts(link+"&");
        return false;
        });
    });
}

function start2(link){
    $("#country ul").load(link+"mode=start",function(){
    $("#country a").click(function(){
        var link=encodeURI($(this).attr("href"));
        posts(link+"&");
        return false;
        });
    });
}

function posts(link){
    $("#posts").load(link+"mode=posts",function(){
    $("a.country").click(function(){
        var link=encodeURI($(this).attr("href"));
        category(link+"&");
        posts(link+"&");
        return false;
        });
    $("a.category").click(function(){
        $("#category").css("display","none");
        var link=encodeURI($(this).attr("href"));
        start2(link+"&");
        posts(link+"&");
        return false;
    });
    });
}

function category(link){
    $("#category ul").load(link+"mode=verNav",function(){
        $("#category").css("display","block");
        $("#category a").click(function(){
        var link=encodeURI($(this).attr("href"));
        posts(link+"&");
        return false;
        });
    });
}

3 个答案:

答案 0 :(得分:4)

当元素已经创建并存在于HTML DOM中时,您需要确保调用jQuery行。换句话说,在页眉头$(document).ready()期间执行此操作

<head>
    ...
    <script>
        $(document).ready(function() {
            // Here.
        });
    </script>
</head>  

或将脚本放在页面正文的底部,感兴趣的元素之后。

<body>
    ...
    <script>
        // Here.
    </script>
</body>

答案 1 :(得分:1)

Works for me。要检查的一些事项:

答案 2 :(得分:1)

确保您没有“alert”作为本地变量或覆盖jQuery“$”。