【问题标题】:How to pass a variable from a link to a jQuery function如何将变量从链接传递到 jQuery 函数
【发布时间】:2009-12-08 07:34:00
【问题描述】:

我希望 jQuery 函数知道单击哪个链接来调用它,即我希望将链接的 id 值传递给 jQuery 函数。

这可能吗?如果是这样,最好的方法是什么。

【问题讨论】:

    标签: javascript jquery argument-passing


    【解决方案1】:

    当然。在click() 事件处理程序中,您可以引用this 单击的元素。

    $("a").click(function() {
      alert(this.id);
      ...
    });
    

    $("a").click(function() {
      alert($(this).attr("id"));
      ...
    });
    

    【讨论】:

    • 谢谢 .... 我真的需要记住“this.id”技术。它基本上解决了我一直问的所有 jQuery 问题。
    【解决方案2】:
    $("a").click(function() { 
       var linkid = $(this).attr("id");
    
       // use linkId here
    });
    

    【讨论】:

      【解决方案3】:

      不要忘记取消默认行为,否则您将一事无成。

      $("a").click(function(e) {
         e.preventDefault();
         var linkid = $(this).attr("id");
         //do whatever here
      });
      

      【讨论】:

        【解决方案4】:
        $("a").click(function() {
          alert($(this).attr("id"));
          ...
        });
        

        我也可以这么说..

        【讨论】:

          猜你喜欢
          • 2011-11-05
          • 2018-04-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多