简体   繁体   English

单击时关闭的可悬停下拉菜单

[英]hover-able drop-down menus that close on click

I have 3 drop-down menus I display them using *ngFor in angular+2 but I'm having a problem of setting all these to close on item click我有 3 个下拉菜单,我在 angular+2 中使用 *ngFor 显示它们,但我遇到了将所有这些设置为在项目单击时关闭的问题

Without close on click没有点击关闭

HTML Code HTML 代码

<div *ngFor="let filter of Filters ; let i = index">
  <span class="dropdown">
    <span class="filter">
      {{ currentFilter[i] }}
    </span>
    <div class="dropdown-content">
      <div class="item" *ngFor="let filterType of filter" (click)="
        currentFilter[i] = filterType;
        onFilter();
      ">
        {{ filterType }}
      </div>
    </div>
  </span>
</div>

CSS CSS

.dropdown-content {
  padding: 8PX;
  display: none;
  position: absolute;
  background-color: #ffffff;
  z-index: 1;
  border-radius: 8px;
  box-shadow: 0 1px 6px 0 rgba(0, 0, 0, 0.26);
  width: 100%;
  font-size: 14px;
  font-weight: 900;
  .item {
    position: relative;
    background-color: Transparent;
    padding: 12px;
    font-size: 16px;
    font-weight: 500;
  }
  .item:hover{
    background-color: #e7eaeb;
    border-left: 3px solid red;
    font-weight: 900;
  }
}
.dropdown:hover 
.dropdown-content {
  display: block ;
}

ts ts

currentFilter: Array<string> = ['ALL', 'ALL', 'ALL', 'ALL'];
Filters : Array<Array<string>> = [
  // offers_type =
  [
    'ALL',
    'DISCOUNT',
    'FREE_DELIVERY',
    'FREE_ITEMS',
  ],

  // activity =
  [
    'ALL',
    'ACTIVE_WITH_IN_TIME',
    'ACTIVE_OUT_OF_TIME',
    'NOT_STARTED',
    'ENDED',
  ],
  // visibility =
  ['ALL', 'VISIBLE', 'HIDDEN'],
];

I Tried to target dropdown-content by id and change the style on click and on hover but this did't work fine with multiple dropdowns in *ngFor我试图通过 id 来定位下拉内容,并在点击和 hover 上更改样式,但这对于 *ngFor 中的多个下拉菜单不起作用

Here is what I tried这是我尝试过的

With close on click点击关闭

<div *ngFor="let filter of Filters ; let i = index">
  <span class="dropdown">
    <span 
      id=i
      class="filter">
      {{ currentFilter[i] }}
    </span>
    <div class="dropdown-content"
         onmouseover="document.getElementById(i).style.display='block';"
     >
      <div class="item" 
        *ngFor="let filterType of filter" 
        (click)="
        currentFilter[i] = filterType;
        onFilter();"
        onClick="document.getElementById(i).style.display='none';"
      >
        {{ filterType }}
      </div>
    </div>
  </span>
</div>

But I'm keeping get this error但我一直收到这个错误

Uncaught TypeError: Cannot read property 'style' of null
    at HTMLDivElement.onmouseover

Uncaught TypeError: Cannot read property 'style' of null
    at HTMLDivElement.onclick 

Note that this code is working fine when targeting a single dropdown with fixed id请注意,当针对具有固定 ID 的单个下拉菜单时,此代码可以正常工作

edited:编辑:

adding ngFor div添加 ngFor div

With the error that you are getting, I can see that you have an error in your code.根据您收到的错误,我可以看到您的代码中有错误。

You have written the following code:您编写了以下代码:

<div class="dropdown-content" onmouseover="document.getElementById(i).style.display='block';">

JavaScript would treat i as a variable, since it is not written as text. JavaScript 会将i视为变量,因为它不是作为文本编写的。 You should use apostrophes with i :您应该在i中使用撇号:

<div class="dropdown-content" onmouseover="document.getElementById('i').style.display='block';">

I have fixed this issue using the edited code bellow in ts file我已经使用 ts 文件中的编辑代码修复了这个问题

  onMouseOver(i){
    document.getElementById(`dropdown-content-${i}`).style.display='block';
  }
  onClick(i){
    document.getElementById(`dropdown-content-${i}`).style.display='none';
  }

in HTML file在 HTML 文件中

    <div *ngFor="let filter of Filters ; let i = index">
      <div>
        <span class="filter-label">
          {{ FiltersLabels[i] | translate }}
        </span>

        <span class="dropdown">
          <span 
            class="filter"
            (mouseover)='onMouseOver(i)'>
            {{ currentFilter[i] }}
          </span>
          <div
            id='item-{{i}}'
            class="dropdown-content"
          >
            <div class="item" 
             *ngFor="let filterType of filter" 
              (click)="
              currentFilter[i] = filterType;
              onFilter();
              onClick(i)
            ">
              {{ filterType }}
            </div>
          </div>
        </span>
      </div>
    </div>

But I still don't know why this worked但我仍然不知道为什么这有效

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

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