简体   繁体   English

将点击事件绑定到我页面上的锚标签

[英]bind click event to anchor tags on my page

Below is my javascript file code. 以下是我的javascript文件代码。 Script.js Script.js

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

And here is my php code. 这是我的PHP代码。

    <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. 我正在警报框中测试Hello。 But it is not working. 但这是行不通的。

Any advice what i am missing. 任何建议我所缺少的。

$("#") isn't a valid selector. $("#")不是有效的选择器。 If you want to capture clicks on anchors it should be $('a') ; 如果要捕获锚点的点击,则应为$('a')

In order to get all the anchor tags you should use $("a") instead of $("#"). 为了获得所有锚标记,您应该使用$(“ a”)而不是$(“#”)。 Use $("#abc") in order to get a tag with id abc. 使用$(“#abc”)以获得ID为abc的标签。 Use $(".def") in order to get tags with class def. 使用$(“。def”)以获得带有def类的标签。

The # in $("#") means and id should follow. #$("#")是指与id应该遵循。 Like if your element's id were "myElement" you would write $("#myElement") . 就像如果您的元素的id是“ myElement”一样,您将编写$("#myElement")

But, since you dont know your ids adead of time, id use the vote class you already have instead like $(".vote").click.... 但是,由于您不知道ID的使用时间,所以ID使用您已经拥有的vote类,例如$(".vote").click....

 $(document).ready(function() { $(".vote").click(function(event) { event.preventDefault(); alert("Hello! My id is: " + this.id); }); }); 
 <script src="https://ajax.googleapis.com/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> 

Use attr name at the selector and call directly the function you want... $('a') is so generic 在选择器上使用attr名称,然后直接调用所需的函数... $('a')非常通用

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!");
});

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

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