因此,我想要实现的是只在某个父页面的子页面上显示一个类,并隐藏在其余的页面上。我查找了它,并了解到我可以通过页面ID使用jQuery实现这一点。但是这个子页面一直在更新,所以有什么方法可以让这个自动更新吗?
.sidebar {示例页和子页;
https://example/event/
https://example/event/pikachu谢谢
发布于 2022-04-26 10:39:00
const { pathname } = window.location例如:
https://www.example.com/path1 <-- Hide the class
https://www.example.com/path1/path2 <-- Show the class
https://www.example.com/path1/path3 <-- Show the class
https://www.example.com/path1/path2/path4 <-- Show the class
etc...// .sidebar class allowed only for sub paths of /path1
// This is the logic //////////////////////////
const { pathname } = window.location;
const target = /^\/path1.*/gim;
if (target.test(pathname) && pathname !== '/path1')
node.classList.add('sidebar');
else node.classList.remove('sidebar');
///////////////////////////////////////////////您可以测试演示这里
https://stackoverflow.com/questions/72012338
复制相似问题