javascript-插入HTML或附加Child onClick,然后从先前单击的元素中删除
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript-插入HTML或附加Child onClick,然后从先前单击的元素中删除,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1855字,纯文字阅读大概需要3分钟。
内容图文
![javascript-插入HTML或附加Child onClick,然后从先前单击的元素中删除](/upload/InfoBanner/zyjiaocheng/669/84d9adaec1524190bd5889619e9ac51d.jpg)
我在容器内有一组div元素,默认情况下显示.div-to-hide,而.div-to-show是隐藏的.
当我单击.set时,.div-to-hide应该隐藏,而.div-to-show应该是可见的.下次单击应将先前单击的元素恢复为其默认状态.
我需要显示.div-to-show上的单击内部按钮.
<div class="container">
<div class="set">
<div class="div-to-hide">Some text</div>
<div class="div-to-show"></div>
</div>
<div class="set">
<div class="div-to-hide">Some text</div>
<div class="div-to-show"></div>
</div>
<div class="set">
<div class="div-to-hide">Some text</div>
<div class="div-to-show"></div>
</div>
</div>
到目前为止,我有这个:
let lastClicked;
$('.container').on('click', function(e) {
if (this == lastClicked) {
lastClicked = '';
$('.div-to-hide').show();
$(this).children('.div-to-hide').hide();
} else {
lastClicked = this;
$('.div-to-hide').hide();
$(this).children('.div-to-hide').show();
$(this).children('.div-to-show').hide();
}
});
无法使其正常工作..我不知道我在想什么…
任何帮助深表感谢!
更新:成功了!感谢大家!
解决方法:
首先,您没有使用delegation($.on()函数上的第二个参数)将.set元素定义为函数内部的this.
如果我理解正确,则要显示单击的最后一个元素,然后隐藏其余元素.您真的不需要知道最后单击哪个按钮来执行此操作
$('.container').on('click', '.set', function (e) {
// Now "this" is the clicked .set element
var $this = $(this);
// We'll get the children of .set we want to manipulate
var $div_to_hide = $this.find(".div-to-hide");
var $div_to_show = $this.find(".div-to-show");
// If it's already visible, there's no need to do anything
if ($div_to_show.is(":visible")) {
$div_to_hide.show();
$div_to_show.hide();
}
// Now we get the other .sets
var $other_sets = $this.siblings(".set");
// This second way works for more complex hierarchies. Uncomment if you need it
// var $other_sets = $this.closest(".container").find(".set").not(this);
// We reset ALL af them
$other_sets.find(".div-to-show").hide();
$other_sets.find(".div-to-hide").show();
});
内容总结
以上是互联网集市为您收集整理的javascript-插入HTML或附加Child onClick,然后从先前单击的元素中删除全部内容,希望文章能够帮你解决javascript-插入HTML或附加Child onClick,然后从先前单击的元素中删除所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。