jquery addclass 到 body 标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13343988/
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 addclass to body tag
提问by Pete Norris
The following script is within a div, does this matter? As it doesn't work. Is the syntax right? Do I need to tell jquery to go up a level in the DOM first? how would I do this?
以下脚本在 div 中,这有关系吗?因为它不起作用。语法正确吗?我是否需要先告诉 jquery 在 DOM 中上升一个级别?我该怎么做?
$(document).ready(function() {
$('body').addClass('diary');
});
Thanks guys.
谢谢你们。
Here is the relevant section in the HTML:
这是 HTML 中的相关部分:
<body>
<header>
<div class="logo">
<a href=""></a>
</div>
<nav class="main-nav">
<a class="first" href="http://www.adrianwaddingham.com">HOME</a>
<a class="second" href="/sheriff">SHERIFF</a>
<a class="third" href="/biography">BIOGRAPHY</a>
<a class="fourth" href="/contact">CONTACT</a>
<a class="fifth" href="/diary">DIARY</a>
</nav>
</header>
<script>
$(document).ready(function() {
$('body').addClass('diary');
});
</script>
<section class="copy-main">
<article>
采纳答案by halilb
I understand that you are viewing the html source to check body class you added. DOM manipulation with javascript is not visible in source code, but you can check it with Developer Tools in your browser(Chrome, Firefox and IE have one).
我了解您正在查看 html 源代码以检查您添加的正文类。使用 javascript 的 DOM 操作在源代码中不可见,但您可以使用浏览器中的开发人员工具进行检查(Chrome、Firefox 和 IE 都有)。
回答by Kirill Ivlev
Your code is perfectly correct since $('body')
will search the whole DOM
你的代码是完全正确的,因为$('body')
会搜索整个 DOM
回答by Kashif
No problem with script. Diary
class will apply to body. Can you share you diary class and please check that jQuery
is loaded correctly, use any development tool like firebug.
脚本没有问题。Diary
类将适用于身体。你能分享你的日记课吗,请检查jQuery
是否正确加载,使用任何开发工具,如 firebug。
Add jQuery file in your page like following where Scripts is folder and jQuery.js
is jQuery file. If you already included this in your page then check with any developer tool that it is loading correctly.
在您的页面中添加 jQuery 文件,如下所示,其中 Scripts 是文件夹并且jQuery.js
是 jQuery 文件。如果您已将其包含在您的页面中,请使用任何开发人员工具检查它是否正确加载。
<script src="Scripts/jQuery.js" type="text/javascript"></script>
Or add from CDN
或者从 CDN 添加
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
回答by Muthu Kumaran
Your code looks fine. Check here, http://jsfiddle.net/muthkum/P5uS2/. Use FireBug or Chrome Inspector to check the <body>
tag.
你的代码看起来不错。检查这里,http://jsfiddle.net/muthkum/P5uS2/。使用 FireBug 或 Chrome Inspector 检查<body>
标签。
Make sure you included jquery
file.
确保您包含jquery
文件。