我有一个网址www.xyz.com/partyevent/zalen
有没有办法获取url mid值,即“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);
});
感谢您的帮助..
答案 0 :(得分:1)
试试这个:
$(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);
});