Jquery href anchor value

为君一笑 提交于 2019-12-18 09:36:09

问题


I am having an anchor link in aspx page like:

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


回答1:


You could do this:

var myTag = $('#Anchor')[0].search.split('=')[1];

Example: http://jsfiddle.net/B6GYB/

Or not using jQuery:

var myTag = document.getElementById('Anchor').search.split('=')[1];

Example: http://jsfiddle.net/B6GYB/1/




回答2:


$(function(ready){
    alert($('#Anchor').attr('href')); // prints Myproject/Mypage.aspx?tag=asp
    alert($('#Anchor').text()); // prints Go
});

http://jsfiddle.net/max6s/




回答3:


To get the href of the link:

var href = $('#Anchor').attr('href');

To get the HTML inside:

var html = $('#Anchor').html();

#Anchor is the CSS-format selector that means, "Select the element with the ID 'Anchor'."




回答4:


You can get the specific query parameter of url by using following code:

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>

and here is the usage of this function:

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>

Usage:

alert($.getAnchorValue('myTag', $('#Anchor').attr('href')));

EDIT: I have editted my answer and also added the jquery code for getting the querystring parameter



来源:https://stackoverflow.com/questions/4059193/jquery-href-anchor-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!