下面的jQuery函数可以正常运行,并根据网址在full
页面上的<body>
HTML元素中添加/signin/
类。
// add full class to sign in page body based on url
$(function() {
var loc = window.location.href; // returns the full URL
if(/signin/.test(loc)) {
$('body').addClass('full');
}
});
我想在前面/主页上完成相同的操作,或者在同一个函数中完成/
URL。我该怎么做?
答案 0 :(得分:1)
正则表达式很慢。更快捷的方式:
function() {
var loc = window.location.pathname; // returns the pathname
if(loc == '' || loc == '/') {
$('body').addClass('full');
}
});
答案 1 :(得分:0)
使用适当的正则表达式。在你的情况下/^\/$/
。
/^\/$/.test('/') // true
/^\/$/.test('/asdf') //false
在您的情况下进一步添加它。我会,使用window.location.pathname
$(function() {
var loc = window.location.pathname; // returns the pathname
if(/^\/$/.test(loc)) {
$('body').addClass('full');
}
});