我需要将背景颜色属性设置为整个身体,但是我的代码似乎不起作用--我正在学习jquery,但我仍然是个新手。
我使用选择器这个来调用body元素,并向它应用样式。
<html>
<head>
<title></title>
<script type="text/javascript" src="jquery-1.11.1.js">
</script>
<script type="text/javascript">
jQuery(document).ready(function(){
$(this).css("background-color","green");
}
);
</script>
</head>
<body>
<ul id="destinations">
<li>First</li>
<li>Second</li>
<li>Third</li>
<li>Fourth</li>
<li>Fifth</li>
<li class="six">Sixth</li>
</ul>
<p>This is another parragraph</p>
<p>and this is another more</p>
</body>
</html
>发布于 2014-11-24 03:40:30
在jQuery中,$(this)引用调用当前函数的DOM元素。由于在文档‘准备’事件中调用$(this),$(this)引用文档(DOM),这就是代码不能工作的原因。
要应用CSS规则,您必须以body标记为目标。因此,您必须更改以下内容:
$(this).css("background-color","green");
对此:
$('body').css("background-color","green");
发布于 2014-11-24 03:40:46
你应该选择主体并在那里设置背景。
$('body').css("background-color","green");发布于 2014-11-24 04:14:59
*如果您想设置可能不会更改的背景色,则可以使用
$('body').css("background-color","color-name");https://stackoverflow.com/questions/27097507
复制相似问题