[英]How to add body class to front page & an internal page using jQuery?
下面的jQuery函數可以正常工作,並根據URL向/signin/
頁面上的<body>
HTML元素添加full
類。
// 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。 我該怎么做呢?
正則表達式很慢。 更快的方法:
function() {
var loc = window.location.pathname; // returns the pathname
if(loc == '' || loc == '/') {
$('body').addClass('full');
}
});
使用適當的正則表達式。 /^\\/$/
在您的情況下。
/^\/$/.test('/') // true
/^\/$/.test('/asdf') //false
根據您的情況進一步添加。 我會使用window.location.pathname
$(function() {
var loc = window.location.pathname; // returns the pathname
if(/^\/$/.test(loc)) {
$('body').addClass('full');
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.