我的 div 中有一些按钮,当按下 1 时,我想更改其他按钮的样式,因此每次只突出显示一个元素。我的问题是因为它们有不同的颜色。因此,具体来说,我需要获取当前 child 的边框颜色并将其分配给自己的颜色。
这是我的例子:
$('.myItem').click(function(
$(this).parent().children().not(this).css('background-color', 'white');
$(this).parent().children().not(this).css('color', $('*').css('border-color'));
)}
有没有办法像我们使用 $(this) 一样获取当前的 child ?
最佳答案
您可以在 children()
上使用 .map()
,然后引用 child
。
$('.myItem').click(function() {
$(this).parent().children().map((id, child) => {
console.log(id, child);
$(child).css('color', '#f00');
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<p class="myItem">click me</p>
<p>greetings</p>
</div>
关于javascript - 有没有办法在使用 children() 时获取当前的 child ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42196890/