How to get the parameters of an href attribute of a link from the click event object

前端 未结 4 1199
没有蜡笔的小新
没有蜡笔的小新 2021-02-15 16:37

Is there a simple way to get the parameters at the end of an href attribute of a clicked link using the click event object?

I have some jQuery code that looks like this:

4条回答
  •  悲哀的现实
    2021-02-15 17:17

    jQuery itself doesn't support URL parsing. However there are lots of jQuery extensions available which do and make this an easy task. For example

    • https://github.com/allmarkedup/jQuery-URL-Parser

    With this extension you can do the following

    $('#pages').delegate("a", "click", function(e) {
      var href = $(this).attr('href');
      var url = $.url(href);
      var query = url.attr('query')
      ...
    });
    

    The extension itself supports much more than just the query string. You can use attr for practically every part of the url.

提交回复
热议问题