javascript – 使用jQuery获取url并提取url段

栏目: jQuery · 发布时间: 6年前

内容简介:翻译自:https://stackoverflow.com/questions/24481918/using-jquery-to-get-url-and-extract-url-segments

在具有类别列表的网页上,每个类别标题都以以下格式链接:http:// localhost / admin / category / unpublish / 2

我编写了以下js代码,尝试捕获url和段’unpublish'(动作)和’2′(id),并需要将请求发送到http:// localhost / admin / category

$('#statusChanges a').click(function(evt) { // use the click event of hyperlinks
  evt.preventDefault();
  var url = $(location).attr('href');
  // var action = url.segment(3);  /*JS console complains that url.segment() method undefined! */
  // var id = url.segment(4);
  $.ajax({
    type: "GET",
    url: $(location).attr('href'),
    dat: '',
    /* do I need to fill the data with json data: {"action": "unpublish, "id": 2 } ? but I don't know how to get the segments */
    success: function(data) {
      $('.statusSuccess').text('success!');
    },
    error: function(data) {
      $('.statusSuccess').text('error!');
    }
  });
}); // end of status change

试试这个

var url = $(location).attr('href').split("/").splice(0, 5).join("/");

更新答案:

使用此对象获取当前锚点链接,如下所示

$(this).attr('href')

翻译自:https://stackoverflow.com/questions/24481918/using-jquery-to-get-url-and-extract-url-segments


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Agile Web Application Development with Yii 1.1 and PHP5

Agile Web Application Development with Yii 1.1 and PHP5

Jeffrey Winesett / Packt Publishing / 2010-08-27

In order to understand the framework in the context of a real-world application, we need to build something that will more closely resemble the types of applications web developers actually have to bu......一起来看看 《Agile Web Application Development with Yii 1.1 and PHP5》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

URL 编码/解码
URL 编码/解码

URL 编码/解码

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具