我需要将背景颜色属性设置为整个主体,但我的代码似乎不起作用 我正在学习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
>
最佳答案
在 jQuery 中,$(this) 指的是调用当前函数的 DOM 元素。由于您在文档“ready”事件中调用 $(this),因此 $(this) 引用文档 (DOM),这就是您的代码不起作用的原因。
要应用 CSS 规则,您必须改为定位 body 标记。因此,您必须更改此设置:
$(this).css("背景颜色","绿色");
对此:
$('body').css("背景颜色","绿色");
关于javascript - 将背景颜色属性设置为整个文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27097507/