angular 中的dom操作(原生JS)
2024-09-01 12:42:25
<h2>这是一个home组件--DOM操作演示</h2>
<div id="box">
this is box
</div>
<br>
<div id="box1" *ngIf="flag">
this is box1
</div>
import { Component, OnInit } from '@angular/core'; @Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.scss']
})
export class HomeComponent implements OnInit { public flag:boolean=true; constructor() { } ngOnInit() { //组件和指令初始化完成 并不是真正的dom加载完成 let oBox:any=document.getElementById('box'); console.log(oBox.innerHTML);
oBox.style.color="red"; //获取不到dom节点
/*
let oBox1:any=document.getElementById('box1'); console.log(oBox1.innerHTML);
oBox1.style.color="blue"; */ } //视图加载完成以后触发的方法 dom加载完成 (建议把dom操作放在这个里面)
ngAfterViewInit(){
let oBox1:any=document.getElementById('box1');
console.log(oBox1.innerHTML);
oBox1.style.color="blue"; } }
效果:
最新文章
- Spring--开山篇
- 【康拓展开】及其在求全排列第k个数中的应用
- 高性能文件缓存key-value存储—Redis
- 【自动部署】Ansible 怎么通过堡垒机/跳板机 访问目标机器
- Maven-006-手动部署第三方构件至 nexus 私服
- [转]http://lua-users.org/wiki/LpegTutorial
- Python3 学习第十二弹: 补充something
- 【LeetCode】204 - Count Primes
- 转载 C#中静态类和非静态类比较
- ksh和bash区别
- 24种设计模式--组合模式【Composite Pattern】
- linux ulimit的使用,如何产生core文件,调试段错误
- mysql用户权限分配及主从同步复制
- linux系统做raid
- nodemanager 无法启动报错“doesn&#39;t satisfy minimum allocations”
- 开发第一个flutter程序 hello world
- 关于Javascript闭包(Closure)
- 为什么研发团队不适合量化KPI的绩效考核?
- Android的Fragment中onActivityResult不被调用
- HTML JavaScript 基础学习
热门文章
- NSIP
- STM32定时器配置(TIM1、TIM2、TIM3、TIM4、TIM5、TIM8)高级定时器+普通定时器,定时计数模式下总结
- about微信小程序
- Caused by: java.lang.ClassNotFoundException: org.fusesource.jansi.WindowsAnsiOutputStream
- ASP.NET MVC 入门5、View与ViewData
- DML子句returing into用法举例
- scoket --- 练习
- 16Vue 表单的输入绑定
- vue生命周期在工作中的用法
- PostgreSQL 不要使用kill -9 杀 Postgresq 用户进程