jQuery 如何获取jquery锚点href值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13926045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:05:48  来源:igfitidea点击:

how to get jquery anchor href value

jqueryanchor

提问by user1911703

jQuery:

jQuery:

$(document).ready(function() {
    $("a.change_status").click(function(){
       var status_id = $("a").val();
       alert(status_id); 
       return false;
    });
});

HTML:

HTML:

<a href="?status=5" class="change_status">Aminul</a><br/>
<a href="?status=25" class="change_status">Arif</a><br/>
<a href="?status=15" class="change_status">Sharif</a><br/>

I need status_idand for some reason my anchor tag is dynamic. I can't use id or make class name dynamic. I think, I need to use $thisto get my value.

我需要status_id并且出于某种原因我的锚标记是动态的。我不能使用 id 或使类名动态化。我想,我需要用$this才能得到我的价值。

回答by hsuk

This one is simple :

这个很简单:

     $(document).ready(function() {
         $("a.change_status").click(function(){
           var status_id = $(this).attr('href').split('=');
           alert(status_id[1]); 
           return false;
        });
    });

http://jsfiddle.net/NmzRV/

http://jsfiddle.net/NmzRV/

回答by Bishnu Paudel

var status_id= $(this).attr("href").match(/status=([0-9]+)/)[1];

回答by r0m4n

You have two separate issues here... first you need to find the actual clicked link using thisand then find the value of that href attribute.

这里有两个单独的问题......首先你需要找到实际点击的链接this,然后找到该 href 属性的值。

$(document).ready(function() {
    $("a.change_status").click(function() {
        var status_id = parseURL($(this).attr("href"));
        alert(status_id);
        return false;
    });
});

Also, because javascript doesn't have a way to pull URL parameters, you must write a function (in the example parseURL) in which to find the value of the variable "status":

此外,由于 javascript 无法提取 URL 参数,因此您必须编写一个函数(在示例中parseURL)来查找变量“status”的值:

function parseURL(theLink) {
    return decodeURI((RegExp('status=' + '(.+?)(&|$)').exec(theLink) || [, null])[1]);
}

See the following jsfiddle:

请参阅以下jsfiddle:

http://jsfiddle.net/ZKMwU/

http://jsfiddle.net/ZKMwU/

回答by Rick

$('a').attr('href');

should do the trick

应该做的伎俩

回答by Surinder ツ

You can do this like :

你可以这样做:

$(document).ready(function() { 
  $("a.change_status").click(function() { 
      var status_id = $(this).attr("href"); 
      alert(status_id);
      return false;
  }); 
});

回答by Sridhar Narasimhan

$(document).ready(function() {
        $("a.change_status").click(function(){
           var status_id = $(this).attr("href");
           alert(status_id); return false;
        });
    });

回答by MikeTedeschi

$("a.change_status").click(function(){ 
     var status_id = $(this).attr('href'); 
     alert(status_id); 
});