Jquery href anchor value

后端 未结 4 594
小蘑菇
小蘑菇 2020-12-21 18:38

I am having an anchor link in aspx page like:

Go
<
相关标签:
4条回答
  • 2020-12-21 18:45

    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

    0 讨论(0)
  • 2020-12-21 18:49

    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'."

    0 讨论(0)
  • 2020-12-21 18:56

    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/

    0 讨论(0)
  • 2020-12-21 19:05
    $(function(ready){
        alert($('#Anchor').attr('href')); // prints Myproject/Mypage.aspx?tag=asp
        alert($('#Anchor').text()); // prints Go
    });
    

    http://jsfiddle.net/max6s/

    0 讨论(0)
提交回复
热议问题