让我澄清我的问题以及我正在寻找的解决方案。我正在使用 wikispaces.com,我想使用 jQuery 动态地为每个页面添加一个唯一的主体类,以某种方式获取 URL,然后插入专门应用于该页面的唯一主体类。
所以,这是我的维基空间中的示例网址...
http://wikithemes.wikispaces.com/Audio+Page
我希望 jQuery 能够抓住...让我们说出 .com/ 之后的第一个单词,在该页面上将是audio。所以,我需要的 jQuery 将应用该类audio到 body 标签,就像这样......
<body class="audio">
在他们的文档中,任何 jQuery 都可以插入,因为它是默认加载的。但他们澄清说,代替 jQuery 中使用的 $ 符号,它只能在 wikispaces 中使用,如果你使用这个词jQuery反而。像这样...
jQuery(document).ready(function(){
我非常感谢您为我的工作提供的任何帮助。谢谢!
不确定放置代码时有什么限制。我会把这个放在<head>
您的文档并将该类应用于html
元素而不是body
所以你不会得到FOUS
或“无样式内容的闪光”。该类几乎在页面加载后立即“出现”在元素上,但如果您这样做,则在显示之前:
<script type="text/javascript">
var loc = window.location.pathname.match(/^\/?(\w+)\b/);
// document.documentElement is the html element, this adds the class
if(loc) document.documentElement.className += " " + loc[1].toLowerCase();
</script>
如果你真的想使用 jQuery:
<script type="text/javascript">
// jQuery is passed to the ready function as a parameter, so we alias it with $
// This will work for you even with wikispaces
jQuery(function($){
var loc = window.location.pathname.match(/^\/?(\w+)\b/);
if(loc) $(document.body).addClass(loc[1].toLowerCase());
});
</script>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)