gpt4 book ai didi

javascript - 如何选择除第一个 div 之外的所有主体元素

转载 作者:行者123 更新时间:2023-11-29 09:55:59 25 4
gpt4 key购买 nike

我有这个 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>

当我单击大字体 时,我想向除我的第一个 div(具有名为 switcher 的 id)之外的所有主体元素添加一个类按钮

我试过了

 $(document).ready(function(){
$("#switcher-large").bind("click", function(){
$("body:not(:first-child)").addClass("large");
});
});

$(document).ready(function(){
$("#switcher-large").bind("click", function(){
$("body:not(div)").addClass("large");
});
});

$(document).ready(function(){
$("#switcher-large").bind("click", function(){
$("body").not("#switcher").addClass("large");
});
});

但似乎都不起作用(该类适用于所有元素)。

我不想找到替代方法,比如只选择 p 元素。我只是想了解为什么我使用的选择器不起作用...

最佳答案

您需要排除 document.body 本身。喜欢

$("body *").not("#switcher").addClass("large"); 

这将查询 body 的所有子项,不包括 #switcher。也许更方便:

$(document.body).contents().not('#switcher').addClass('large');

关于javascript - 如何选择除第一个 div 之外的所有主体元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11647137/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com