gpt4 book ai didi

javascript - 无法使用 .not() 或 :not properly

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:24:14 24 4
gpt4 key购买 nike

为什么我无法将 .red 类添加到 .out 中,但排除了最接近的类?

$('.ring').on('click', function() {

$(this).not().closest('out').addClass('red');
});
div {
height: 100px;
width: 100px;
float: left;
margin: 2px;
}

.ring {
background: grey;
}

.out {
background: khaki;
}

.red {
background: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="map">
<div class="data">
<div class="ring"></div>
</div>
<div class="out"></div>
</div>
<div class="map">
<div class="data">
<div class="ring"></div>
</div>
<div class="out"></div>
</div>
<div class="map">
<div class="data">
<div class="ring"></div>
</div>
<div class="out"></div>
</div>
<div class="map">
<div class="data">
<div class="ring"></div>
</div>
<div class="out"></div>
</div>
<div class="map">
<div class="data">
<div class="ring"></div>
</div>
<div class="out"></div>
</div>

最佳答案

我会像这样使用 index 来做到这一点:

$('.ring').on('click', function() {
$(".red").removeClass("red");
var index = $(".ring").index(this);
$(".out").not(":eq("+index+")").addClass('red');
});
div {
height: 100px;
width: 100px;
float: left;
margin: 2px;
}

.ring {
background: grey;
}

.out {
background: khaki;
}

.red {
background: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="map">
<div class="data">
<div class="ring"></div>
</div>
<div class="out"></div>
</div>
<div class="map">
<div class="data">
<div class="ring"></div>
</div>
<div class="out"></div>
</div>
<div class="map">
<div class="data">
<div class="ring"></div>
</div>
<div class="out"></div>
</div>
<div class="map">
<div class="data">
<div class="ring"></div>
</div>
<div class="out"></div>
</div>
<div class="map">
<div class="data">
<div class="ring"></div>
</div>
<div class="out"></div>
</div>

关于javascript - 无法使用 .not() 或 :not properly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52656605/

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