zoukankan      html  css  js  c++  java
  • angular获取dom节点

    angular获取dom节点

    .html文件中

    <div #myBox>
      dom节点
    </div>
    

    在.ts文件中
    引入 ViewChild

    import { ViewChild } from '@angular/core';
    
    export class NewsComponent implements OnInit {
    //写在类里面
      @ViewChild("myBox") myBox: any
      //ngAfterViewInit生命周期函数里面获取dom
      ngAfterViewInit(): void {
        console.log(this.myBox.nativeElement.innerHTML)
        this.myBox.nativeElement.style.width = "100px";
        this.myBox.nativeElement.style.height = "100px";
        this.myBox.nativeElement.style.background = "red";
      }
    
    }
    

    获取一个子组件

    可以使用里面的方法。。。。。
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    请用今天的努力,让明天没有遗憾。
  • 相关阅读:
    高级打字机
    不等数列
    间谍网络
    医院设置
    题目编号生成器
    传纸条
    聪明的打字员
    倒水问题
    八数码难题
    生日蛋糕
  • 原文地址:https://www.cnblogs.com/cupid10/p/15617649.html
Copyright © 2011-2022 走看看