简体   繁体   English

使用jQuery在Angular 4中遍历DOM

[英]Traversing the DOM in Angular 4 with jQuery

I need help in updating the CSS for selectedElement, currently, this works but only for the first element. 我需要帮助来更新selectedElement的CSS,目前,此方法有效,但仅适用于第一个元素。

Basically, I have a link, when I click on it: 基本上,当我单击它时,我有一个链接:

(click)="showUserDetails($event)"

I'm passing the x & y coordinates to my method and want to use this to update the css and enabled a boolean flag which shows the div: 我正在将x和y坐标传递给我的方法,并希望使用它来更新css并启用显示div的布尔标志:

  showUserDetails(el: any) {
let target = el.target || el.srcElement;
  let selectUsername = (document.getElementById('userDetailsInfo') as HTMLInputElement);
  selectUsername.style.top = target.offsetY + "px";
  selectUsername.style.left = target.offsetX + "px";
  this.selected = true;
  el.preventDefault();

} }

Currently, the above code only seems to work for the first element and I'm trying to figure out a way to have it so based on the clicked element, go up the dom and find me the div with the id of 'userDetailsInfo' and apply the top/left values to that, and not just to the first item. 目前,以上代码似乎仅适用于第一个元素,我正在尝试找到一种方法,使其基于clicked元素,进入dom并找到ID为'userDetailsInfo'的div,然后将顶部/左侧的值应用于该值,而不仅仅是第一项。

I actually solved it like so: 我实际上是这样解决的:

  private hostEl: HTMLElement;

  constructor(el: ElementRef) {
      this.hostEl = el.nativeElement;
  }

  ngOnInit() {
    window.addEventListener('scroll', this.scroll, true);
  }

  ngOnDestroy() {
      window.removeEventListener('scroll', this.scroll, true);
  }

  //Get the header
  let header = (document.querySelector('.header') as HTMLInputElement);

  if (window.pageYOffset >= 100){
      header.classList.add('sticky');
  } else {
      header.classList.remove('sticky');
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM