gpt4 book ai didi

javascript - Jquery Url 拆分并获取中间值

转载 作者:行者123 更新时间:2023-12-03 12:36:40 25 4
gpt4 key购买 nike

我有一个网址 www.xyz.com/partyevent/zalen

有没有办法获取 url 中间值,即“partyevent”并吐出其前后的部分。我想要获得中间值“partyevent”以将类添加到正文中。

 $(document).ready(function () { 
var url = window.location.pathname;
var count = url.match(new RegExp("/", 'g'));
var urlsplit = url.split("/");
var page_class = urlsplit[count.length];

alert(page_class);
$('body').addClass(page_class);
});

感谢您的帮助..

最佳答案

试试这个:

$(function () { 
var url = window.location.pathname;
var urlsplit = url.split("/"); // this returns array of ["","partyevent",zalen]
var page_class = urlsplit[1]; // you need not bother about count when u need the word just after the domain

alert(page_class);
$('body').addClass(page_class);
});

关于javascript - Jquery Url 拆分并获取中间值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23709100/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com