当我单击其中一个按钮时,<p>s
仅在封装 div 中的所有按钮都应为黄色背景。
理想情况下,我希望在 jquery 选择器中查看和匹配元素选择器$(this)
。parent
更新:抱歉,我没有明确提出我的要求。
我想查看父 div 的选择器、我想在父项中选择的元素以及$(this)
所有在 select 子句中的元素。
假设您想从中获取<p>
元素.left div
,您可以像这样扩展选择器子句
$("p", ".left")
- Gets all <p>
in.left
在我的情况下,我想.left
使用$(this)
, 而这个“是”按钮。应该在
$("p", $(this).closest("body > div"))
这是 jsFiddle
代码。
<style>
div p.painted {
background-color: #FF0;
}
</style>
<div class="left">
<div class="actions">
<div class="ui">
<div class="colors">
<button class="paint">Paint it yellow!</button>
</div>
</div>
</div>
<p>
Lorem Ipsum
</p>
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
</div>
<div class="right">
<div class="actions">
<div class="ui">
<div class="colors">
<button class="paint">Paint it yellow!</button>
</div>
</div>
</div>
<p>
Lorem Ipsum
</p>
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
</div>
<script>
$(function(){
$(".paint").click(PaintIt);
});
function PaintIt(){
$(this).closest("p").addClass("painted");
}
</script>