![](/img/trans.png)
[英]How can I dynamically add bootstrap alerts to a page using javascript?
[英]How can I dynamically add a class to 'body' using JavaScript only for the home page?
我将此添加到头部,但它不起作用:
<script>
var xpathname = (window.location.pathname);
if (xpathname ==('/')) {
$('body').addClass('home');
}
</script>
该网站在这里: http : //xotdr.unxpr.servertrust.com/
不幸的是,Volusion 不允许开发人员自由编码,因此我需要实施很多解决方法。
编辑:我希望该课程仅显示在主页body
。
由于您将此添加到head
您需要在body
标记可用时执行此代码段:
$(function() {
var xpathname = window.location.pathname;
if (xpathname == '/') {
$('body').addClass('home');
}
});
<script>
var bodyclass=document.createAttribute("class");
bodyclass.value="home";
document.getElementsByTagName("body")[0].setAttributeNode(bodyclass);
</script>
试试这个
var b = document.getElementsByTagName('body')[0];
b.className += 'home';
我知道这是一个旧帖子,但这个问题仍然有用。
var xpathname = (window.location.pathname);
var ndeBody = document.getElementsByTagName("body")[0];
if (xpathname ==('/')) {
ndeBody.classList.toggle("home");
}
else{
ndeBody.classList.toggle("home");
}
当我转到该 URL 时,您有语法错误:
Uncaught ReferenceError: x$ is not defined
例如,你要删除的x
中的x$('body').addClass('home');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.