javascript - 两次点击 html/jQuery 注册
问题描述
我打印了几次具有不同 id 的 HTML 部分代码(${element.id}
在每个部分中它们都是不同的)。现在第一个子元素可以正常工作,但其他子元素会单击两次(单击一次后它的淡入和淡出)。我想删除这个错误。我使用 Laravel 作为主要框架。对不起我的英语:) 提前谢谢!
<div class="notification warning closeable" id="checkbox-area">
<input type="checkbox" name="blocks[]" class="form-check-input" id="element${element.id}" value="${element.url}" checked>
<label class="form-check-label" for="element${element.id}">${element.name}
<img src="${window.location.origin}/${element.thumbnail}" id="original" class="imgBlocks">
</label>
<button class="dropbtn" data-id="${element.id}" type="button">...</button>
</div>
<div class="drop-down" id="drop_${element.id}">
<div id="text-generation" class="drop-down-text">
</div>
<button type="button" id="btn-text-updater">new text</button>
</div>
const textGen = document.getElementById('text-generation');
const btnUpdateTextGen = document.getElementById('btn-text-updater');
let countTextRefresh = 0;
$(dotsBtn).click(function (e) {
e.preventDefault();
let id = $(this).attr("data-id")
if ( $("#drop_"+id).is(":visible") ) {
$("#drop_"+id).fadeOut(1000);
} else {
$("#drop_"+id).fadeIn(1000);
}
})
解决方案
你应该改变:
if ( $("#drop_"+id).is(":visible") ) {
$("#drop_"+id).fadeOut(1000);
} else {
$("#drop_"+id).fadeIn(1000);
}
至
if ( $("#drop_"+id).is(":visible") ) {
$("#drop_"+id).fadeOut(1000);
return;
}
$("#drop_"+id).fadeIn(1000);
推荐阅读
- reactjs - 阿波罗的 MockedProvider 没有明确警告丢失的模拟
- parsing - 如何在实例化上选择词法分析器/解析器规则
- xamarin.forms - xamarin 选择器不绑定
- mysql - 添加 INDEX 以加快查询的最佳解决方案是什么?
- r - 如何制作一个计算 R 中 f(x) = exp(x) 的牛顿商的函数?
- deployment - 更新 local_settings.py 后出现 500 个错误
- javascript - Django 和 navigator.serviceWorker.register 仅在本地工作
- typescript - 如何将具有泛型的函数类型应用于函数
- apache-kafka - 远程客户端使用时卡夫卡出错
- c# - 将 TimeSpan 格式化为小时、分钟和秒