简体   繁体   English

单击按钮时隐藏 div 并显示 div

[英]Hide divs and display div when button clicked

I am a total newbie of Jquery and JavaScript.我是 Jquery 和 JavaScript 的新手。 I am using this code as reference: https://jsfiddle.net/6uzU6/2/我使用此代码作为参考: https : //jsfiddle.net/6uzU6/2/

What I am trying to do is similar as the link above, which is to display a div after a corresponding button is clicked while others divs are hidden.我试图做的类似于上面的链接,即在单击相应按钮后显示一个 div,而其他 div 被隐藏。

I was trying to modify the code to use div instead of unordered list.我试图修改代码以使用 div 而不是无序列表。

The problem I am having right now is when I use Brave Browser to open the page, the divs are not hiding and no changes are made after clicking the button.我现在遇到的问题是,当我使用 Brave Browser 打开页面时,div 没有隐藏,单击按钮后也没有进行任何更改。 While using Chrome, the page will be blank, nothing is shown.使用 Chrome 时,页面将为空白,不显示任何内容。

tutorialIndex.html教程索引.html

{% extends "morse_logs/base.html" %}

{% block content %}
{% load static %}
<link rel="stylesheet" href="{% static 'morse_logs/css/style.css' %}">
<link href="{% static 'morse_logs/js/jquery-ui/jquery-ui.min.css' %}" rel="stylesheet">
<link href="{% static 'morse_logs/js/jquery-ui/jquery-ui.structure.min.css' %}" rel="stylesheet">
<link href="{% static 'morse_logs/js/jquery-ui/jquery-ui.theme.min.css' %}" rel="stylesheet">

<div class="container">
    <h1>Tutorial</h1>
</div>

<div id="menu">
    <button class="justClick" href="#">A</button>
    <button class="justClick" href="#">B</button>
    <button class="justClick" href="#">C</button>
    <button class="justClick" href="#">D</button>
</div>

<div class="content">

</div>

    <div class="content1">
        <h1>A</h1>
        <img src="{% static 'morse_logs/img/A.png' %}" alt="A" style="width:128px;height:128px;">
    </div>
    <div class="content2">
        <h1>B</h1>
    </div>
    <div class="content3">
        <h1>C</h1>
    </div>
    <div class="content4">
        <h1>D</h1>
    </div>



<script src="{% static 'morse_logs/js/jquery-3.4.1.min.js' %}"></script>
<script src="{% static 'morse_logs/js/jquery-ui/jquery-ui.js' %}"></script>
<script src="{% static 'morse_logs/js/app.js' %}"></script>
{% endblock content %}

app.js应用程序.js

$('div').not(".content").hide();
$('.justClick').bind('click', function() {
    $('div').not(".content").hide();
    $('div.content').html($('div.content' + ($(this).index()+1)).html());
});

Not sure about your code but here's another way to do it.不确定您的代码,但这是另一种方法。

Its easy to read and understand...它易于阅读和理解......

 $(document).ready(function(){ $(".justClicka").click(function() { $(".content1").css("display", "block"); $(".content2,.content3,.content4").css("display", "none"); }); $(".justClickb").click(function() { $(".content2").css("display", "block"); $(".content1,.content3,.content4").css("display", "none"); }); $(".justClickc").click(function() { $(".content3").css("display", "block"); $(".content1,.content2,.content4").css("display", "none"); }); $(".justClickd").click(function() { $(".content4").css("display", "block"); $(".content1,.content2,.content3").css("display", "none"); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link rel="stylesheet" href="{% static 'morse_logs/css/style.css' %}"> <link href="{% static 'morse_logs/js/jquery-ui/jquery-ui.min.css' %}" rel="stylesheet"> <link href="{% static 'morse_logs/js/jquery-ui/jquery-ui.structure.min.css' %}" rel="stylesheet"> <link href="{% static 'morse_logs/js/jquery-ui/jquery-ui.theme.min.css' %}" rel="stylesheet"> <div class="container"> <h1>Tutorial</h1> </div> <div id="menu"> <button class="justClicka" href="#">A</button> <button class="justClickb" href="#">B</button> <button class="justClickc" href="#">C</button> <button class="justClickd" href="#">D</button> </div> <div class="content"> </div> <div class="content1" style="display:none"> <h1>A</h1> <img src="{% static 'morse_logs/img/A.png' %}" alt="A" style="width:128px;height:128px;"> </div> <div class="content2" style="display:none"> <h1>B</h1> </div> <div class="content3" style="display:none"> <h1>C</h1> </div> <div class="content4" style="display:none"> <h1>D</h1> </div>

Page is blank because $('div').not(".content").hide() hides ALL divs except the one with the class="content" including the container and menu divs页面是空白的,因为$('div').not(".content").hide()隐藏了除class="content"之外的所有 div,包括containermenu div

Change it to $('div.content ~ div').hide()将其更改为$('div.content ~ div').hide()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM