如何通过单击按钮隐藏div?

时间:2022-08-26 20:02:42

In my angular.js learning project I want to hide one div and show another when I click a button. In this code, I'd like the first div to be hidden at the click (or even destroyed?) and the second div to be shown. Basically I want the user experience of going from page 1 to page 2 in my app. How do I make this happen?

在我的angular.js学习项目中,我想隐藏一个div并在单击按钮时显示另一个div。在这段代码中,我希望第一个div隐藏在点击(甚至被破坏?)和第二个div显示。基本上我想要在我的应用程序中从第1页到第2页的用户体验。我该如何做到这一点?

Index.html

的index.html

  <ion-content ng-controller="StartpageCtrl">

      <div class="list card" id="startCard" ng-show="showstartCard">
          <div class="item item-image item item-text-wrap">
              Card 1
          </div>
      </div>
      <div class="list card" id="secondCard" ng-show="showsecondCard">
          <div class="item item-image item item-text-wrap">
              Card 2
          </div>
      </div>

      <button ng-click="hideCard()" class="button button-full button-calm button icon-right ion-chevron-right">
          Start now
      </button>

  </ion-content>

controllers.js

controllers.js

.controller("StartpageCtrl", funcion($scope){
    $scope.showstartCard = true;
    $scope.showsecondCard = false;

    $scope.hideCard = function() {
        $scope.showstartCard = false;
        $scope.showsecondCard = true;
    };
});

When I run the page I see "Card 2" and nothing happens when I click the button. What I wanted to see was "Card 1" and for it to switch to "Card 2" as I click the button...

当我运行页面时,我看到“卡2”,当我点击按钮时没有任何反应。我想看到的是“卡1”并且当我点击按钮时它切换到“卡2”...


Solved I had forgotten to add references to controllers.js in the app.js angular.module as well as the script tag in index.html. It works fine now.

解决了我忘记在app.js angular.module中添加对controllers.js的引用以及index.html中的脚本标记。它现在工作正常。

2 个解决方案

#1


1  

Using angular:   
 HTML file:
    <td><a href="#" ng-click="showDetails =! showDetails">Source Doc..</a>
                <div id="mydiv" ng-show="showDetails">
                    <div id="mydiv-container">
                        <div id="mydiv-content">
                            <a href="#" ng-click="showDetails =! showDetails">Close</a>
                            <br>
                            hello
                        </div>
                    </div>
                </div>

                </td>



    css file:
    body {
        height: 100%;
        background-color: #F0F0F0;
        font-family: Arial, sans-serif;
    }
    #mydiv {
        width: 100%;
        height: 100%;
        overflow: hidden;
        left: 100px;
        top: 150px;
        position: absolute;
        opacity: 0.95;

    }
    #mydiv-container {
        margin-left: auto;
        margin-right: auto;
    }
    #mydiv-content {
        width: 70%;
        padding: 20px;
        background-color: white;
        border: 1px solid #6089F7;
    }
    a {
        color: #5874BF;
        text-decoration: none;
    }
    a:hover {
        color: #112763;
    }

#2


0  

If you are looking for toggle, then this code might help as there is no need to have any extra CSS or JS functions needed.

如果您正在寻找切换,那么这段代码可能有所帮助,因为不需要任何额外的CSS或JS功能。

<button ng-click="isDetailOpen = !isDetailOpen"
        ng-class="!isDetailOpen ? 'ion-ios-arrow-down' : 'ion-ios-arrow-up'"
        class="button button-clear button-positive icon-right">
  <span ng-show="!isDetailOpen">More Detail</span>
  <span ng-show="isDetailOpen">Less Detail</span>
</button>
<div ng-show="isDetailOpen">
  <p>Per ad ferri dicta, omnis laudem dicunt duo an. Ex pri solum definitiones.</p>
</div>

I figured it out while working on Angular-Ionic project.

我在开展Angular-Ionic项目时想到了这一点。

#1


1  

Using angular:   
 HTML file:
    <td><a href="#" ng-click="showDetails =! showDetails">Source Doc..</a>
                <div id="mydiv" ng-show="showDetails">
                    <div id="mydiv-container">
                        <div id="mydiv-content">
                            <a href="#" ng-click="showDetails =! showDetails">Close</a>
                            <br>
                            hello
                        </div>
                    </div>
                </div>

                </td>



    css file:
    body {
        height: 100%;
        background-color: #F0F0F0;
        font-family: Arial, sans-serif;
    }
    #mydiv {
        width: 100%;
        height: 100%;
        overflow: hidden;
        left: 100px;
        top: 150px;
        position: absolute;
        opacity: 0.95;

    }
    #mydiv-container {
        margin-left: auto;
        margin-right: auto;
    }
    #mydiv-content {
        width: 70%;
        padding: 20px;
        background-color: white;
        border: 1px solid #6089F7;
    }
    a {
        color: #5874BF;
        text-decoration: none;
    }
    a:hover {
        color: #112763;
    }

#2


0  

If you are looking for toggle, then this code might help as there is no need to have any extra CSS or JS functions needed.

如果您正在寻找切换,那么这段代码可能有所帮助,因为不需要任何额外的CSS或JS功能。

<button ng-click="isDetailOpen = !isDetailOpen"
        ng-class="!isDetailOpen ? 'ion-ios-arrow-down' : 'ion-ios-arrow-up'"
        class="button button-clear button-positive icon-right">
  <span ng-show="!isDetailOpen">More Detail</span>
  <span ng-show="isDetailOpen">Less Detail</span>
</button>
<div ng-show="isDetailOpen">
  <p>Per ad ferri dicta, omnis laudem dicunt duo an. Ex pri solum definitiones.</p>
</div>

I figured it out while working on Angular-Ionic project.

我在开展Angular-Ionic项目时想到了这一点。