【问题标题】:How to pass URL for dynamically generated anchor tags to jQuery hover() function??如何将动态生成的锚标签的 URL 传递给 jQuery hover() 函数?
【发布时间】:2013-03-14 12:24:42
【问题描述】:

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

<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 对特定 URL 的调用。所以我正在为这个id注册一个hover()这样的函数:

$('#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、链接的位置等。

对于单个锚标记,它可以工作,但对于 multiplle 则不行。请帮助我。提前致谢。

【问题讨论】:

    标签: jquery html anchor


    【解决方案1】:

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

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

    【讨论】:

    【解决方案2】:

    您应该使用类而不是对多个对象使用相同的 id,并为每个对象使用 this.href。不允许将相同的 id 分配给多个 html 元素,但您可以这样做。

    <a href="/path/100" id="clickme" class="someclass">One link</a>
    <a href="/path/101" id="clickme" class="someclass">Sec link</a>
    <a href="/path/102" id="clickme" class="someclass">Third link</a>
    <a href="/path/103" id="clickme" class="someclass">Fourth link</a>
    
    
    $('.someclass').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
                }
              });
    });
    

    【讨论】:

      【解决方案3】:

      如果您的悬停事件处理程序获取 href 属性,您可以在内部使用$(this)。其他答案关于使用类而不是 id 是正确的。下面的示例设置为使用“clickme”类而不是 id。

      $('.clickme').hover(function(){
           var $this = $(this);
           var theUrl = $this.attr('href');
      
           $.ajax({
                  beforeSend : function(jqXHR){
                      //Doin something
                  },
                  url: theUrl
                  success: function(data) {
                      //Doin something
                  },
                  error: function(jqXHR){
                      //Doin something
                  }
                });
          });
      

      【讨论】:

        【解决方案4】:

        它不起作用,因为 ID 是唯一的,jQuery 只会找到 ID 的第一个实例。

        <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>
        

        js

        $('.clickme').mouseenter(function(){
            var href = $(this).attr('href');
            $.ajax({
                 url : href
            });
        });
        

        【讨论】:

        • 使用mouseenter 比hover 有什么优势??
        • @Anubhab - jQuery 的 hover() 函数只是 mouseenter 和 mouseleave 的一个快捷方式,你只使用了 mouseenter 部分,所以为什么不干脆放弃额外的函数调用,直接使用 mouseenter。跨度>
        • 我使用的是 1.4.2 版本。您能否提出一种不同的处理方式?因为on功能是在1.7版本中添加的
        • 是的,我可以做到..我找到了另一个解决方案,而不是hover,我正在使用mouseenter。 :)
        • @Anubhab - 哎呀,您是否阅读了上面的答案并注意到它使用了什么事件?
        猜你喜欢
        • 1970-01-01
        • 2012-02-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-19
        • 1970-01-01
        • 2015-03-01
        • 2021-02-11
        相关资源
        最近更新 更多