【问题标题】:ionic app ,on click of a button not work离子应用程序,单击按钮不起作用
【发布时间】:2017-11-15 14:33:27
【问题描述】:

我正在开发一个应用程序,这是我遇到问题的第二个屏幕。 有一个跳过按钮,当我按下它时,“其中包含文本的 div”应该隐藏。

我在打字稿文件“home.ts”中编写了代码,并在 home.html 中编写了它的 html,在 home.css 中编写了 css

我只用这些代码完成了 2 个屏幕,一个是启动屏幕,另一个是免责声明或主屏幕,我试图在单击跳过按钮时用文本隐藏 div

这里是 home.ts 代码:

import { Component,ViewChild} from '@angular/core';
import { NavController } from 'ionic-angular';


@Component({
  selector: 'page-home',
  templateUrl:'home.html'
})
export class HomePage {
@ViewChild('username')username;
@ViewChild('password')password;

  splash = true;

  constructor(public navCtrl: NavController ) {
    }
    showhide(){

      var toggle = function() {
      var mydiv = document.getElementById('div');
      if (mydiv.style.display === 'block' || mydiv.style.display === ''){
        mydiv.style.display = 'none';
      }
      else{
        mydiv.style.display = 'block';
      }
      }
    }

  ionViewDidLoad() {
    setTimeout(() => this.splash = false, 4000);

  }  




    }



and here is home.html file

<html>
        <head>
            <meta charset="utf-8">
            <meta name="viewport" content="initial-scale=1,maximum-scale=1, user-scalable=no">
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
            <script src="lib/jquery-1.11.1.js"></script>
            <script src="lib/ionic/js/ionic.bundle.js"></script>
            <script src="js/app.js"></script>
            <script src="js/controller.js"></script>
            <script src="js/show.js"></script>
            <script src="build/animation.css"></script>
        </head>
             <body ng-controller="imageshow_hide">
                    <ion-content>
                            <div id="custom-overlay"[style.display]="splash ? 'flex': 'none'">
                                    <div class="flb">
                                            <div class="Aligner-item Aligner-item--top">
                                            </div>
                                            <img src="assets/logo.png">
                                            <div class="Aligner-item Aligner-item--bottom">
                                            </div>
                                    </div>
                            </div>
                                <div class="scroll-content">
                                                <img class="image1" src="assets/bg2.jpg"/>

                                                <div id="hides" class="image2"><img src="assets/whitetop.png"/>
                                                        <h2 style="text-align:center">WHY SHOULD I USE THIS APP

                                                        </h2>
                                                                <p>Our thoughts and everything we see feel hear taste and smell
                                                                shapes the unconscious mind.
                                                                By listening to affirmations daily.guided meditation and performing simple NLP techniques
                                                                you can re-program your mind to focus on the results you desire in life and develop
                                                                positive patterns for motivation and action
                                                                </p>

                                                </div>
                                                <div id="hides" class="image3"><img src="assets/whitebottom.png"/>
                                                    <h2 style="text-align:center">HOW TO USE THIS APP</h2>
                                                    <p>Don't listen to any of these recordings 
                                                        while operating machinery or driving.only listen to these materials if you
                                                        are in safe place where you can relax.
                                                        All contents provided within their applications are NOT meant to replace 
                                                        any qualified medical treatment or health related advice.
                                                        If you suffer from any mental disorder,NOT Listen to this material.
                                                        MindVolution accepts No responsibility or liability for any injury,loss or damage 
                                                        as direct or indirect result of the usage of the information here presented.
                                                    </p>

                                                </div>
                                                    <button (click)="showhide()" id="hides">
                                                    <img class="image4" ng-hide="checked" src="assets/skip.png"/>
                                                    </button>
                                                <img class="image5"src="assets/sound icon.png"/>
                                                <img class="image6"src="assets/sound icon.png"/>
                                </div>
                    </ion-content>
                </body>
    </html>

【问题讨论】:

    标签: javascript html css angularjs ionic-framework


    【解决方案1】:

    您正在寻找一个 id 为“div”的 div,但您的 html 中没有,您是否打算使用 id“hidden”?那么你的代码应该是 var mydiv = document.getElementById('hidden')

    实现此目的的更简单方法是在 home.ts 中创建一个布尔变量,例如isVisible,使用 *ngIf 在您想要依赖于它的值是否显示的标签上,然后在用户单击按钮后更新它的值。 例如

    <div *ngIf="isVisible">Content to hide when user clicks the button</div>
    <button (click)="isVisible = !isVisible;"></button>
    

    隐藏闪屏是使用this.platform.ready().then(()={this.splashScreen.hide();});的好习惯,顺便说一下,你似乎没有导入和使用闪屏原生插件,你只是给变量splash一个值。

    【讨论】:

    • 我在 home.ts 文件中使用这个 showhide(){ var toggle = function() { var mydiv = document.getElementById('hidden'); if (mydiv.style.display === 'block' || mydiv.style.display === ''){ mydiv.style.display ='none'; } else{ mydiv.style.display ='block'; } } }
    • Hidden 是我想在单击按钮时隐藏的两个 div 的 id(此按钮内部有图像)。此按钮具有 id =skipbtn 这是跳过按钮的 html 代码,用户将在其中按下隐藏包含图像的两个 div
    • 这里有两个 div 需要在点击跳过按钮时隐藏

      为什么我应该使用这个应用程序

      跨度>
    • 如何使用这个APP

      不要听这些录音

    • 你不应该为不同的元素使用相同的id,getElementById只返回一个元素,如果你想选择多个元素(在这种情况下是两个div)你应该使用一个类然后使用getElementsByClassName ,这将返回一个包含它找到的所有元素的数组,然后您可以对其进行迭代并更改显示属性。
    【解决方案2】:
    I am using this in my home.ts file
    
    showhide(){
    
          var toggle = function() {
          var mydiv = document.getElementById('hidden');
          if (mydiv.style.display === 'block' || mydiv.style.display === ''){
            mydiv.style.display ='none';
          }
          else{
            mydiv.style.display ='block';
          }
          }
        }
    
    Hidden is the id of two divs which i want to hide on click of button ( this button has an image inside it).this button has id =skipbtn
    
    
    Here is html code of skip button where user will press to hide the tow divs containing image
    
    <button click="showhide()" >
                                                        <img class="image4" id="skipbtn" ng-hide="checked" src="assets/skip.png"/>
                                                        </button>
    
    Here are two divs which need to  be hide on click of skip button
    
    <div class="scroll-content">
                                                    <img class="image1" src="assets/bg2.jpg"/>
    
                                                    <div id="hidden" class="image2"><img src="assets/whitetop.png"/>
                                                            <h2 style="text-align:center">WHY SHOULD I USE THIS APP
    
                                                            </h2>
                                                                        <p>Our thoughts and everything we see feel hear taste and smell
                                                                        shapes the unconscious mind.
                                                                        By listening to affirmations daily.guided meditation and performing simple NLP techniques
                                                                        you can re-program your mind to focus on the results you desire in life and develop
                                                                        positive patterns for motivation and action
                                                                    </p>
                                                    </div>
                                                    <div id="hidden" class="image3"><img src="assets/whitebottom.png"/>
                                                        <h2 style="text-align:center">HOW TO USE THIS APP</h2>
                                                        <p>Don't listen to any of these recordings 
                                                            while operating machinery or driving.only listen to these materials if you
                                                            are in safe place where you can relax.
                                                            All contents provided within their applications are NOT meant to replace 
                                                            any qualified medical treatment or health related advice.
                                                            If you suffer from any mental disorder,NOT Listen to this material.
                                                            MindVolution accepts No responsibility or liability for any injury,loss or damage 
                                                            as direct or indirect result of the usage of the information here presented.
                                                        </p>
                                                    </div>
    

    【讨论】:

    • 这应该回答你的问题吗?如果是这样,您需要解释为什么它是一个答案,如果不是,您应该删除它并通过编辑问题将任何必要的信息合并到您的问题中。
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签