I have this HTML:
我有这个HTML:
<body>
<div id="switcher" class="switcher">
<h3>Style Switcher</h3>
<button id="switcher-default">
Default
</button>
<button id="switcher-narrow">
Narrow Column
</button>
<button id="switcher-large">
Large Print
</button>
</div>
<p>asdfasdfasdfas dsadf</p>
<p>asd'lfkasdf</p>
<script src="jquery.js"></script>
<script src="test.js"></script>
</body>
I want to add a class to all body elements except my first div
(which has the id
called switcher), when I click on the Large print button
.
当我点击Large print按钮时,我想在除了我的第一个div(其id为cuther)之外的所有body元素中添加一个类。
I have tried
我试过了
$(document).ready(function(){
$("#switcher-large").bind("click", function(){
$("body:not(:first-child)").addClass("large");
});
});
and
$(document).ready(function(){
$("#switcher-large").bind("click", function(){
$("body:not(div)").addClass("large");
});
});
and
$(document).ready(function(){
$("#switcher-large").bind("click", function(){
$("body").not("#switcher").addClass("large");
});
});
but none seem to work (the class applies to all elements).
但似乎没有工作(该类适用于所有元素)。
I don't want to find an alternate way like selecting only p
elements. I just want to understand why the selectors I used don't work...
我不想找到另一种方法,比如只选择p元素。我只是想明白为什么我使用的选择器不起作用......
2 个解决方案
#1
4
You need to exclude the document.body
itself. Like
您需要排除document.body本身。喜欢
$("body *").not("#switcher").addClass("large");
This will query for all children of body
, excluding #switcher
. Maybe more convinient:
这将查询身体的所有孩子,不包括#switcher。也许更方便:
$(document.body).contents().not('#switcher').addClass('large');
#2
1
$("body").children().not("#switcher").addClass("large");
#1
4
You need to exclude the document.body
itself. Like
您需要排除document.body本身。喜欢
$("body *").not("#switcher").addClass("large");
This will query for all children of body
, excluding #switcher
. Maybe more convinient:
这将查询身体的所有孩子,不包括#switcher。也许更方便:
$(document.body).contents().not('#switcher').addClass('large');
#2
1
$("body").children().not("#switcher").addClass("large");