最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - bind click event to anchor tags on my page - Stack Overflow

programmeradmin2浏览0评论

Below is my javascript file code. Script.js

$(document).ready(function(){ 
$("#").click(function(){
    alert("Hello!");
});

And here is my php code.

    <html>
    <head>
Some codes are here
    </head>
    <body>
    Some php codes are here to get value from database
    <script src="script.js"></script>
        <span><a href="#" class="vote" id="<?php echo $cid; ?>" name="up"><i class="fa fa-thumbs-up"></i></a> <?php echo $up; ?> </span>

             <span><a href="#" class="vote" id="<?php echo $cid; ?>" name="down"><i class="fa fa-thumbs-down"></i></a> <?php echo $down; ?> </span>

        <span><a href="#" class="vote" id="<?php echo $cid; ?>" name="favorite"><i class="fa fa-star-o"></i></a> <?php echo $fav; ?> </span>
        </body>
    </html>                             

I am calling script file in body due to my design. I can't call it in head section.

Issue is when i click on above link corresponding to # (up,down, fav). It take me to index file. I am testing Hello in alert box. But it is not working.

Any advice what i am missing.

Below is my javascript file code. Script.js

$(document).ready(function(){ 
$("#").click(function(){
    alert("Hello!");
});

And here is my php code.

    <html>
    <head>
Some codes are here
    </head>
    <body>
    Some php codes are here to get value from database
    <script src="script.js"></script>
        <span><a href="#" class="vote" id="<?php echo $cid; ?>" name="up"><i class="fa fa-thumbs-up"></i></a> <?php echo $up; ?> </span>

             <span><a href="#" class="vote" id="<?php echo $cid; ?>" name="down"><i class="fa fa-thumbs-down"></i></a> <?php echo $down; ?> </span>

        <span><a href="#" class="vote" id="<?php echo $cid; ?>" name="favorite"><i class="fa fa-star-o"></i></a> <?php echo $fav; ?> </span>
        </body>
    </html>                             

I am calling script file in body due to my design. I can't call it in head section.

Issue is when i click on above link corresponding to # (up,down, fav). It take me to index file. I am testing Hello in alert box. But it is not working.

Any advice what i am missing.

Share Improve this question edited May 24, 2015 at 4:24 Wesley Smith 19.6k22 gold badges91 silver badges134 bronze badges asked May 24, 2015 at 2:56 RoxxRoxx 3,99624 gold badges100 silver badges165 bronze badges 1
  • Change $("#").click( to $("a").click(. Just # is not a selector. – Shaunak D Commented May 24, 2015 at 2:58
Add a ment  | 

4 Answers 4

Reset to default 7

$("#") isn't a valid selector. If you want to capture clicks on anchors it should be $('a');

The # in $("#") means and id should follow. Like if your element's id were "myElement" you would write $("#myElement").

But, since you dont know your ids adead of time, id use the vote class you already have instead like $(".vote").click....

$(document).ready(function() {
      $(".vote").click(function(event) {
        event.preventDefault();
        alert("Hello! My id is: " + this.id);
      });
  
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<span><a href="#" class="vote" id="someId1" name="up"><i class="fa fa-thumbs-up"></i>Some sontent</a></span>

<span><a href="#" class="vote" id="someId2" name="down"><i class="fa fa-thumbs-down"></i>Some sontent</a> </span>

<span><a href="#" class="vote" id="someId3" name="favorite"><i class="fa fa-star-o"></i>Some sontent </a> </span>

In order to get all the anchor tags you should use $("a") instead of $("#"). Use $("#abc") in order to get a tag with id abc. Use $(".def") in order to get tags with class def.

Use attr name at the selector and call directly the function you want... $('a') is so generic

try to use

$("a[name='up']").on('click',function(){
    alert("Clicked up!");
});

$("a[name='down']").on('click',function(){
    alert("Clicked down!");
});

$("a[name='favorite']").on('click',function(){
    alert("Clicked down!");
});
发布评论

评论列表(0)

  1. 暂无评论