在 jQuery 中,我们有 position()
method :
$("button").click(function(){
var x = $("p").position();
alert("Top: " + x.top + " Left: " + x.left);
});
它将返回元素的位置。我怎样才能在 Angular 中做同样的事情?
最佳答案
请尝试:
在component.html中,定义对html标签的引用:
<p #position>I want to go back here</p>
在组件.ts中:
@ViewChild('position') HoldPosition: ElementRef;
clickFunction() {
console.log(this.HoldPosition.nativeElement.offsetTop);
console.log(this.HoldPosition.nativeElement.offsetLeft);
}
```
关于javascript - 像 jQuery 位置方法一样获取 Angular 中的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51523770/