开发者

Link calling a jQuery function doesn't seem to be working

I am a jQuery newb so this is probably rudimentary.

I have a test page here: http://www.problemio.com where I am trying to make links to vote up or down.

Here is how the links look like:

<a class="link_button" id="vote_up">Vote Up</a> 
<a class="link_button" id="vote_down">Vote Down</a>

I use the class attribute for styling all over the site with that link_button so I decided to use the id attribute to distinguish the link for the jQuery call.

Here is how my jQuery function looks like:

$('a.vote_up').click(function() 
{
alert("up");
});

Right now I just 开发者_StackOverflow中文版want to make sure it is getting called properly. But the alert isn't getting called which is making me think this is broken somewhere. Any idea how I can get this working?

Thanks!


You're using the class selector . instead of the id selector #. Hence do either

$('#vote_up').click(function(){
     alert("up");
});

or

$('.link_button').click(function(){ 
    if( this.id === 'vote_up' )
        alert('vote up'); 
    else if ( this.id === 'vote_down' )
        alert('vote down'); 
 } 

EDIT: also make sure everything is in a $(document).ready... i.e

$(function(){ 
    $('#vote_up').click(function(){
         alert("up");
    });
}); 


Your current selector is incorrect, it is looking for an a tag with a class of vote_up not it's id...

use this instead...

$(function() {
    $('#vote_up')...
});


Have you tried the id selector?

$('#vote_up').click(function() {
    alert('up');
});


a.vote_up looks for an <a> tag with the class vote_up. In your HTML vote_up is an ID, so you want this:

$('#vote_up').click(function() 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜