jQuery Jquery从body标签中删除类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8468996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery to remove class from body tag
提问by 916 Networks
My page is dynamically generating the body class, and it's some WordPress plugin generating. I wanted to easily remove that class, so figured I could strip with jQuery. I thought this would remove the class "page" from the body code which ends up looking like:
我的页面是动态生成 body 类的,它是一些 WordPress 插件生成的。我想轻松地删除那个类,所以我想我可以用 jQuery 剥离。我认为这会从正文代码中删除类“页面”,最终看起来像:
<body class="page otherattr otherattr2 etc etc">
By using this jquery in my footer:
通过在我的页脚中使用这个 jquery:
<script type="text/javascript">
$(document).ready(function(){
$("body").removeClass("page");
});
</script>
But it doesn't seem to work, am I missing something?
但它似乎不起作用,我错过了什么吗?
回答by Nandhakumar
$(document).ready(function(){
jQuery(window).load(function () {
$("body").removeClass("page");
});
});
回答by Gabriele Petrioli
If with dynamicallyyou mean in the back-end (server side) then your code should work..
如果动态地意味着在后端(服务器端),那么您的代码应该可以工作..
If you mean with some other javascript then you should make sure that your code is run after the code that adds it..
如果您的意思是使用其他一些 javascript,那么您应该确保您的代码在添加它的代码之后运行。
On way would be to add a delay, or even better to keep checking until it is added..
方法是添加延迟,或者甚至更好地继续检查直到添加。
Something like
就像是
$(function cleanBody(){
var body = $('body');
if ( body.is('.page') ){
body.removeClass('page');
} else {
setTimeout(cleanBody, 500);
}
});
should do it..
应该这样做..
回答by Francis Kim
Works for me 100%. Please check the source code of 'Result' iframe on the bottom right.
100% 对我有用。请检查右下角“结果”iframe 的源代码。
回答by Sasi Dhar
Try to add your script at the bottom of the page and check whether it is working fine. If it is the case then the class "page" is getting appended from another javascript code. Try finding it and place your removal code after that.
尝试在页面底部添加您的脚本并检查它是否工作正常。如果是这种情况,则“页面”类将从另一个 javascript 代码中附加。尝试找到它并在此之后放置您的删除代码。