我刚刚了解到关键字“nativeElement”不兼容跨浏览器,例如:
let whatever = <HTMLDivElement>this.$elementRef.nativeElement;
whatever.style.display = 'none';
现在可以通过像这样使用 render 方法来更正。
this._render.setElementClass(whatever, 'display', 'none');
但是我还有以下内容:
this.whatever = <HTMLDivElement>this.$elementRef.nativeElement.querySelector(".myLookupClass");
请告知是否在这种情况下
".nativeElement.querySelector(".myLookupClass")"
跨浏览器工作?如果不是,请建议替代技术。
提前致谢。
最佳答案
您的代码将适用于跨浏览器。首先,当您编写以下代码时,您需要了解真正给您带来 Angular 东西:
this.$elementRef.nativeElement
Angular 将为您提供在 DOM 上呈现的组件的引用。这意味着 Angular 提供了一个普通的 javascript 对象,你可以在任何浏览器上使用这个对象。所以继续使用你的代码。这将在任何浏览器上得到支持。
关于javascript - nativeElement 对象,DOM 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48148929/