我有一個按鈕和另一個div與一些內容。我想在用戶點擊按鈕時將焦點設置到div。我知道在JavaScript中我可以通過document.getElementById(「divID」)。focus();但是在打字稿中有任何方法。如何使用Angular with TypeScript對點擊按鈕聚焦一個div塊
0
A
回答
0
可以在HTML
<button (click)='focus()'> focus</button>
使用@ViewChild
@ViewChild('selector') selector: ELementRef;
focus() {
this.selector.nativeElement.focus()
}
和2的方法將是
<sole-element #el> <sole-element>
<button (click)='focus(el)'> focus</button>
or
<button (click)='el.focus()'> focus</button>
和TS文件
focus(el) {
el.nativeElement.focus()
}
+0
這個問題的前提是錯誤的。 TypeScript方式是根據定義的JavaScript方式。因此,這個答案不適用於問如何在TypeScript中做到這一點,而不是如何在Angular中做到這一點 –
0
試試這個:
在HTML文件:
<div>
<button class="btn btn-primay" (click)="onFocus()">onFocus</button>
</div>
<div>
<input type="text" name="name" #focusContent>
</div>
component.ts
import { Component, ElementRef, ViewChild } from '@angular/core';
export class AppComponent {
@ViewChild('focusContent') el: any;
onFocus() {
this.el.nativeElement.focus();
}
}
相關問題
- 1. UIImagePickerController with cameraOverlayView在點擊疊加中的按鈕時會聚焦
- 2. 按鈕無法點擊或可聚焦
- 3. 單擊按鈕將文本複製到另一個div with angularjs
- 4. 如何使硒點擊一個按鈕
- 5. TodoMVC with angular-typescript
- 6. 如何使用Angular JS聚焦輸入
- 7. tvOS:強制一個按鈕被聚焦
- 8. 如果焦點在下一個div上,取消聚焦事件
- 9. Angular:用戶點擊Enter時如何點擊模式按鈕?
- 10. 使用VBA點擊一個按鈕
- 11. 聚焦點擊的按鈕在Android活動
- 12. 通過箭頭鍵點擊防止按鈕被聚焦
- 13. 焦點第一個錯誤字段按鈕點擊asp.net mvc
- 14. Angular js按鈕點擊
- 15. 在點擊按鈕後將焦點放在特定的div上
- 16. 如何通過點擊一個按鈕
- 17. 給一個按鈕鼠標點擊焦點
- 18. 如何避免聚焦一個div,當它是點擊並拖動..?
- 19. c#selenium點擊div按鈕
- 20. WatiN點擊一個按鈕
- 21. 可點擊按鈕表的麻煩,需要防止雙擊聚焦按鈕,同時保持以下可點擊
- 22. 使用jQuery獲得一個div沒有點擊其他按鈕
- 23. 使用點擊一個按鈕
- 24. 使用ACRA點擊一個按鈕
- 25. 對焦點DIV的點擊操作
- 26. 如何使用Angular在點擊按鈕時激活CSS動畫?
- 27. 顯示一個DIV對應按鈕點擊
- 28. 焦點股利按鈕後點擊
- 29. 如何顯示隱藏按鈕點擊jquery下一個div?
- 30. 如何通過點擊一個按鈕打開不同的div?
這個問題的前提是錯誤的。 TypeScript方式是根據定義的JavaScript方式。 –
@AluanHaddad前提已改變。他使用角標籤顯然是爲了角度。 – alexKhymenko