溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

在Angular中快速定位DOM元素的方法

發(fā)布時(shí)間:2021-02-22 10:35:54 來源:億速云 閱讀:129 作者:小新 欄目:web開發(fā)

這篇文章主要介紹了在Angular中快速定位DOM元素的方法,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

在使用Angular2+中,經(jīng)常會(huì)想快速的去選擇DOM上的某個(gè)元素,如果是剛上手Angular,有可能直接就使用原生DOM操作或者導(dǎo)入jQuery再進(jìn)行DOM操作,既然都使用了Angular了,有沒有更好的方法呢?答案是肯定的。

通過ElementRef

先上代碼:

import {Component, ElementRef, OnInit} from '@angular/core';
@Component({
 selector: 'app-root',
 templateUrl: './app.component.html',
 styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit{
 color:string;
 title = 'button !';
 constructor(private el:ElementRef){}
 setHeight(){
  this.el.nativeElement.querySelector('.btn1').style.height = '300px';
 }
 ngOnInit(){
  this.setHeight();
 }
}
<h2>
 {{title}}
</h2>
<div>
<button myHighlight class="btn btn1">按鈕一</button>
<button myHighlight class="btn">按鈕二</button>
<button myHighlight class="btn">按鈕三</button>
</div>

效果是這樣:

在Angular中快速定位DOM元素的方法

上述代碼中的nativeElement其實(shí)包含的是組件中所有的DOM元素,如下圖所示:

在Angular中快速定位DOM元素的方法

通過調(diào)用querySelectorAPI就能獲取頁面元素,需要注意的是querySelector只返回第一個(gè)元素,當(dāng)你需要選擇多個(gè)元素的時(shí)候可以使用querySelectorAll。

通過@viewChild

<h2>
 {{title}}
</h2>
<div>
<button myHighlight class="btn btn1">按鈕一</button>
<button myHighlight class="btn" #btn>按鈕二</button> <!--增加一個(gè)變量-->
<button myHighlight class="btn">按鈕三</button>
</div>
import {Component, ElementRef, OnInit, ViewChild} from '@angular/core';
@Component({
 selector: 'app-root',
 templateUrl: './app.component.html',
 styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit{
 @ViewChild('btn') btn:ElementRef;//通過@ViewChild獲取元素
 color:string;
 title = 'button !';
 constructor(private el:ElementRef){}
 setHeight(){
  this.el.nativeElement.querySelector('.btn1').style.height = '300px';
 }
 setWidth(){
  this.btn.nativeElement.style.width = '200px';//定義寬度
 }
 ngOnInit(){
  this.setHeight();
  this.setWidth();
 }
}

效果如下:

在Angular中快速定位DOM元素的方法

如果多個(gè)HTML元素都定義了相同的變量,使用@viewChild時(shí)只能選擇到第一個(gè)元素。

更好的方法是配合renderer2對(duì)象提供的API去實(shí)現(xiàn)同樣的效果,這樣減少應(yīng)用層與渲染層之間強(qiáng)耦合關(guān)系:

import {Component, ElementRef, OnInit, Renderer2, ViewChild} from '@angular/core';
@Component({
 selector: 'app-root',
 templateUrl: './app.component.html',
 styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit{
 @ViewChild('btn') btn:ElementRef;
 color:string;
 title = 'button !';
  //初始化renderer2
 constructor(private el:ElementRef,private renderer2: Renderer2){}
 setHeight(){
  this.el.nativeElement.querySelector('.btn1').style.height = '300px';
 }
 setWidth(){
  // this.btn.nativeElement.style.width = '200px';
  
  //使用renderer2的setStyle方法設(shè)置寬度
  this.renderer2.setStyle(this.btn.nativeElement,'width','200px')
 }
 ngOnInit(){
  this.setHeight();
  this.setWidth();
 }
}

參考文章中都提到了@viewChild配合renderer選擇元素,但是在Angular4renderer已經(jīng)廢棄掉了,變成了renderer2。

renderer2API中還有其他的一些方法可以用來進(jìn)行一些DOM操作:

class Renderer2 {
 data : {[key: string]: any}
 destroy() : void
 createElement(name: string, namespace?: string) : any
 createComment(value: string) : any
 createText(value: string) : any
 destroyNode : (node: any) => void |
 appendChild(parent: any, newChild: any) : void
 insertBefore(parent: any, newChild: any, refChild: any) : void
 removeChild(parent: any, oldChild: any) : void
 selectRootElement(selectorOrNode: string|any) : any
 parentNode(node: any) : any
 nextSibling(node: any) : any
 setAttribute(el: any, name: string, value: string, namespace?: string) : void
 removeAttribute(el: any, name: string, namespace?: string) : void
 addClass(el: any, name: string) : void
 removeClass(el: any, name: string) : void
 setStyle(el: any, style: string, value: any, flags?: RendererStyleFlags2) : void
 removeStyle(el: any, style: string, flags?: RendererStyleFlags2) : void
 setProperty(el: any, name: string, value: any) : void
 setValue(node: any, value: string) : void
 listen(target: 'window'|'document'|'body'|any, eventName: string, callback: (event: any) => boolean | void) : () => void
}

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“在Angular中快速定位DOM元素的方法”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI