gpt4 book ai didi

javascript - 使用 jquery 切换多个元素类

转载 作者:行者123 更新时间:2023-11-30 09:51:42 25 4
gpt4 key购买 nike

我已经创建了多个表单输入字段,当用户单击输入字段时,这些字段将具有隐藏的弹出窗口。

Fiddle HERE

工作原理:

当用户单击输入字段一时,类 is-displayed 被添加到 div parent protect-field-container 中。当用户单击输入字段二时,类 is-displayed 从元素一中删除并添加到元素二。

我似乎无法让此功能按照我希望的方式工作。下面是我的代码片段。

Jquery :

$('.popUp-block').on("click", function(){
var $popUp = $(this).closest('.protect-field-container')

if ($popUp.hasClass('is-displayed')) {
$popUp.toggleClass('is-displayed');
} else {
$popUp.addClass('is-displayed');
}
});

HTML :

<div class="field-container">
<div class="protect-field-container two-cols col-md-4">

<article class="protect-field planner-form">
<label for="#">Who shall we think about ? <i class="fa fa-question-circle"></i></label>
<input type="text" class="characters popUp-block">
<i class="valid-checked fa fa-check-circle"></i>
<i class="invalid-checked fa fa-times-circle"></i>
</article>

<div class="protect-popup">
<h3>Permanent Total and Partial Disability</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation.</p>
<a href="#">More information</a>
<span>&euro; 100,000 - &euro; 50,000</span>
</div>

</div>
</div>

最佳答案

Working fiddle

在这种情况下您不必使用切换,您可以在用户单击时从所有容器中删除类 is-displayed 并将其添加到当前单击的字段容器中,例如:

$('.popUp-block').on("click",function() {
//Remove class from all the containers
$('.protect-field-container').removeClass('is-displayed');

//Add class to the current clicked container
$(this).closest('.protect-field-container').addClass('is-displayed');
});

或者你可以通过使用show/hide函数来实现,检查下面的例子。

希望这对您有所帮助。


$('.protect-popup').hide();

$('.popUp-block').on("click",function() {
$('.protect-popup').hide();
$(this).closest('.protect-field-container').find('.protect-popup').show();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="field-container">
<div class="protect-field-container two-cols col-md-4">
<article class="protect-field planner-form">
<label for="#">Who shall we think about ? <i class="fa fa-question-circle"></i></label>
<input type="text" class="characters popUp-block">
<i class="valid-checked fa fa-check-circle"></i>
<i class="invalid-checked fa fa-times-circle"></i>
</article>
<div class="protect-popup">
<h3>Permanent Total and Partial Disability</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation.</p>
<a href="#">More information</a>
<span>&euro; 100,000 - &euro; 50,000</span>
</div>
</div>

<div class="protect-field-container two-cols col-md-4">
<article class="protect-field planner-form">
<label for="#">How many childern do have ? <i class="fa fa-question-circle"></i></label>
<input type="text" name="" class="characters popUp-block">
<i class="valid-checked fa fa-check-circle"></i>
<i class="invalid-checked fa fa-times-circle"></i>
</article>
<div class="protect-popup">
<h3>Permanent Total and Partial Disability</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation.</p>
<a href="#">More information</a>
<span>&euro; 100,000 - &euro; 50,000</span>
</div>
</div>
<div class="protect-field-container two-cols col-md-4">
<article class="protect-field planner-form">
<label for="#">Who shall we think about ? <i class="fa fa-question-circle"></i></label>
<input type="text" class="characters popUp-block">
<i class="valid-checked fa fa-check-circle"></i>
<i class="invalid-checked fa fa-times-circle"></i>
</article>
<div class="protect-popup">
<h3>Permanent Total and Partial Disability</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation.</p>
<a href="#">More information</a>
<span>&euro; 100,000 - &euro; 50,000</span>
</div>
</div>
</div>

关于javascript - 使用 jquery 切换多个元素类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36134924/

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