Jquery href 锚点值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4059193/
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
Jquery href anchor value
提问by ANP
I am having an anchor link in aspx page like:
我在 aspx 页面中有一个锚链接,例如:
<a id="Anchor"class="myAnchor" href="Myproject/Mypage.aspx?myTag=asp">Go</a>
I need to access the "myTag" value using jquery.How to do this?
我需要使用 jquery.How 来访问“myTag”值?
回答by Marwelln
$(function(ready){
alert($('#Anchor').attr('href')); // prints Myproject/Mypage.aspx?tag=asp
alert($('#Anchor').text()); // prints Go
});
回答by user113716
You could do this:
你可以这样做:
var myTag = $('#Anchor')[0].search.split('=')[1];
Example:http://jsfiddle.net/B6GYB/
示例:http : //jsfiddle.net/B6GYB/
Or not using jQuery:
或者不使用 jQuery:
var myTag = document.getElementById('Anchor').search.split('=')[1];
Example:http://jsfiddle.net/B6GYB/1/
回答by PleaseStand
To get the href of the link:
要获取链接的 href:
var href = $('#Anchor').attr('href');
To get the HTML inside:
要获取其中的 HTML:
var html = $('#Anchor').html();
#Anchor
is the CSS-format selector that means, "Select the element with the ID 'Anchor'."
#Anchor
是 CSS 格式选择器,意思是“选择 ID 为 'Anchor' 的元素。”
回答by Zain Shaikh
You can get the specific query parameter of url by using following code:
您可以使用以下代码获取 url 的具体查询参数:
Javascript
Javascript
<script type="text/javascript"> function getAnchorValue(anchorId, key) { var href = document.getElementById(anchorId).getAttribute('href'); var pageQuerySearch = new PageQuery(href.split('?')[1]); return unescape(unescape(pageQuerySearch.getValue(key))); } function PageQuery(query) { if (query.length > 1) {this.q = query; } else { this.q = null; } this.keyValuePairs = new Array(); if (this.q) { for (var i = 0; i < this.q.split("&").length; i++) { this.keyValuePairs[i] = this.q.split("&")[i]; } }; this.getValue = function (s) { for (var j = 0; j < this.keyValuePairs.length; j++) { if (this.keyValuePairs[j].split("=")[0] == s) { return this.keyValuePairs[j].split("=")[1]; } } return false; }; } </script>
<script type="text/javascript"> function getAnchorValue(anchorId, key) { var href = document.getElementById(anchorId).getAttribute('href'); var pageQuerySearch = new PageQuery(href.split('?')[1]); return unescape(unescape(pageQuerySearch.getValue(key))); } function PageQuery(query) { if (query.length > 1) {this.q = query; } else { this.q = null; } this.keyValuePairs = new Array(); if (this.q) { for (var i = 0; i < this.q.split("&").length; i++) { this.keyValuePairs[i] = this.q.split("&")[i]; } }; this.getValue = function (s) { for (var j = 0; j < this.keyValuePairs.length; j++) { if (this.keyValuePairs[j].split("=")[0] == s) { return this.keyValuePairs[j].split("=")[1]; } } return false; }; } </script>
and here is the usage of this function:
这是此功能的用法:
alert(getAnchorValue('Anchor', 'myTag'));
alert(getAnchorValue('Anchor', 'myTag'));
JQuery
查询
<script type="text/javascript"> ; (function ($) { $.extend({ getAnchorValue: function (name, url) { function getQueryStringParams() { var parameters = {}, e, a = /\+/g, r = /([^&=]+)=?([^&]*)/g, d = function (s) { return decodeURIComponent(s.replace(a, " ")); }, q = url ? url.split('?')[1] : window.location.search.substring(1); while (e = r.exec(q)) { parameters[d(e[1])] = d(e[2]) }; return parameters; } if (!this.params) this.params = getQueryStringParams(); return this.params[name]; } }); })(jQuery); </script>
<script type="text/javascript"> ; (function ($) { $.extend({ getAnchorValue: function (name, url) { function getQueryStringParams() { var parameters = {}, e, a = /\+/g, r = /([^&=]+)=?([^&]*)/g, d = function (s) { return decodeURIComponent(s.replace(a, " ")); }, q = url ? url.split('?')[1] : window.location.search.substring(1); while (e = r.exec(q)) { parameters[d(e[1])] = d(e[2]) }; return parameters; } if (!this.params) this.params = getQueryStringParams(); return this.params[name]; } }); })(jQuery); </script>
Usage:
用法:
alert($.getAnchorValue('myTag', $('#Anchor').attr('href')));
alert($.getAnchorValue('myTag', $('#Anchor').attr('href')));
EDIT: I have editted my answer and also added the jquery code for getting the querystring parameter
编辑:我编辑了我的答案,并添加了用于获取查询字符串参数的 jquery 代码