如何将动态生成的锚标记的URL传递给jQuery hover()函数? [英] How to pass URL for dynamically generated anchor tags to jQuery hover() function??

查看:79
本文介绍了如何将动态生成的锚标记的URL传递给jQuery hover()函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在这里做的是,我正在生成HTML中的链接列表作为锚标记:

Hi What I am trying to do here is I am generating a list of Links in HTML as Anchor tags:

<a href="/path/100" id="clickme">One link</a>
<a href="/path/101" id="clickme">Sec link</a>
<a href="/path/102" id="clickme">Third link</a>
<a href="/path/103" id="clickme">Fourth link</a>

当有人将鼠标悬停在任何链接上时,我想向特定的网址触发Ajax调用.因此,我正在为此ID注册一个hover()函数:

I want to fire Ajax call to the specific URL when someone hovers on any of the link. So I am registering a hover() function like this for this id:

$('#clickme').hover(function(){
         $.ajax({
                beforeSend : function(jqXHR){
                    //Doin something
                },
                url: //should be the url from the anchor tag that fired this event(how to get it??),
                success: function(data) {
                    //Doin something
                },
                error: function(jqXHR){
                    //Doin something
                }
              });
        });

我的问题是如何将锚标记作为对象或其他东西传递,这样我就可以检索想要的任何内容,例如href,链接的位置等.

My Question is How can I pass the anchor tag as an object or something so that i can retrieve anything i want like href, position of the link etc..

对于单个锚标签,它可以工作,但对于多重锚,则不行.请帮助我.预先感谢.

For single anchor tag it is working but for multiplle not..Please help me. Thanks in advance.

推荐答案

id应该始终是唯一的(这就是为什么将其称为ID)..使它成为类并使用类选择器

id should always be unique (that is why it is called ID) .. make it class and use class selector

html

<a href="/path/100" class="clickme">One link</a>
<a href="/path/101" class="clickme">Sec link</a>
<a href="/path/102" class="clickme">Third link</a>
<a href="/path/103" class="clickme">Fourth link</a>

jquery

$('.clickme').hover(function(){
     var $this=$(this);
     $.ajax({
            beforeSend : function(jqXHR){
                //Doin something
            },
            url: //should be the url from the anchor tag that fired this event(how to get it??),
            data:{'href':$this.attr('href')}, //this will send '/path/100' as href if u click first link 
            success: function(data) {
                //Doin something
            },
            error: function(jqXHR){
                //Doin something
            }
          });
    });    

这篇关于如何将动态生成的锚标记的URL传递给jQuery hover()函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆