简体   繁体   English

链接调用jQuery函数似乎不起作用

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

I am a jQuery newb so this is probably rudimentary. 我是jQuery newb,所以这可能是基本的。

I have a test page here: http://www.problemio.com where I am trying to make links to vote up or down. 我在这里有一个测试页: http : //www.problemio.com ,我尝试在其中建立投票赞成或反对的链接。

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. 我使用class属性通过该link_button在整个网站上进行样式设置,因此我决定使用id属性来区分jQuery调用的链接。

Here is how my jQuery function looks like: 这是我的jQuery函数的样子:

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

Right now I just 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 # . 而不是ID选择器# 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... ie 编辑:还要确保所有内容都在$(document).ready ...即

$(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... 您当前的选择器不正确,它正在寻找a带有vote_up 的标签, vote_up不是它的id ...

use this instead... 改用这个...

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

Have you tried the id selector? 您是否尝试过ID选择器?

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

a.vote_up looks for an <a> tag with the class vote_up . a.vote_up查找具有vote_up类的<a>标记。 In your HTML vote_up is an ID, so you want this: 在您的HTML中, vote_up是一个ID,因此您vote_up

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM