我有3个div,里面有内容(图片)。当你点击其中一个内容时,它变得可见,另外两个内容变得隐藏。我已经设法通过jQuery代码来实现这一点(你可以在下面检查它),但是它是硬编码的坏事,还有其他方法可以吗?jquery在没有硬编码的情况下显示/隐藏div的内容
P.S .:“https://solomia-gera.github.io/”是网站本身,如果你想看看。
---------------------------------------------- - - - - - -代码 - - - - - - - - - - - - - - - - - - - ----------------
<!-- Hide/Show DIV1 content-->
<script>
// Hide content in div with id#mox when cliked on div with id#content
$("#mox2").on("click", function() {
$("#content1").hide();
});
$("#mox3").on("click", function() {
$("#content1").hide();
});
// Show content in div with id#mox when cliked on div with id#content
$("#mox1").on("click", function() {
$("#content1").show();
});
</script>
<!-- Hide/Show DIV2 content-->
<script>
$("#content2").hide();
// Hide content in div with id#mox when cliked on div with id#content
$("#mox1").on("click", function() {
$("#content2").hide();
});
$("#mox3").on("click", function() {
$("#content2").hide();
});
// Show content in div with id#mox when cliked on div with id#content
$("#mox2").on("click", function() {
$("#content2").show();
});
</script>
<!-- Hide/Show DIV3 content-->
<script>
$("#content3").hide();
// Hide content in div with id#mox when cliked on div with id#content
$("#mox2").on("click", function() {
$("#content3").hide();
});
$("#mox1").on("click", function() {
$("#content3").hide();
});
// Show content in div with id#mox when cliked on div with id#content
$("#mox3").on("click", function() {
$("#content3").show();
});
这个答案是伟大的,我已经实现你的方法在我的代码中,一切正常,但有一件事很烦人。出于某种原因,在页面加载时,我会在1/10秒左右看到div2和div3的内容,并且屏幕抖动非常糟糕。我只在Chrome上注意到这一点,Firefox运行良好。它可能是什么? P.S .:我用代码更新了网站,以便您查看该错误。 –
@ r.hrytskiv只有在页面加载时才会隐藏div2和div3,这可能会导致类似的问题。由于页面上的脚本总是在页面加载时隐藏div2和div3,所以无论如何,您应该删除该脚本的部分内容('$(“#content2”)。hide(); $(“#content3”)。hide );'),而是向div2和div3添加'style =“display:none;”',这样它们就不会在加载时显示。 –
太棒了!不再闪烁,谢谢你! –