如何从 ionic4 中的 ion-select 中删除小插入符

How to remove small caret from ion-select in ionic4

我想从 ion-select 中删除内置的灰色小插入符,然后使用我的 改为自定义插入符号(箭头)。

代码:

ion-select {
  color: grey;
  background:url("/assets/resources/img/ArrowDownConfig.svg");
}

但是我的 css 代码无法优先于离子(内置)。

可以看到图中有两个箭头,一个是内置的,一个是自定义的。我想删除内置(离子)一个。

要修改图标,您可以尝试这样的操作:
.select-icon-inner { border-top: transparent!important; }

我不知道如何解决,遇到了同样的问题,但似乎是 DOM Shadow

的问题

如果你有什么发现,也请告诉我,谢谢。

更新: 找到一些

更新#2

我创建了指令以访问 Shadow DOM 并且它能够将样式添加到隔离 DOM。

HTML:

 <ion-select appStyle>

指令(需要找到更好的实现):

    constructor(private el: ElementRef) {

    setTimeout(() => {
        this.el.nativeElement.shadowRoot.querySelector('.select-icon-inner').setAttribute('style', 'display: none !important');
    }, 3000);
}

如果有多个离子-select项,这里是一个示例。

TS代码:

ionViewDidEnter() {
    // ion-select customizing
    const ionSelects = document.querySelectorAll('ion-select');
    let img = null;
    ionSelects.forEach((ionSelect) => {
      const selectIconInner = ionSelect.shadowRoot.querySelector('.select-icon').querySelector('.select-icon-inner');
      if(selectIconInner){
        selectIconInner.attributes.removeNamedItem("class");
        img = document.createElement("img");
        img.src = "./new-arrow-down-image.svg";
        img.style.width = "12px";
        img.style.paddingTop = "3px";
        img.style.paddingLeft = "4px";
        img.style.color = "black";
        img.style.opacity = "0.5";
        selectIconInner.appendChild(img);
      }
    });
}

如果您只想删除插入符号,可以这样做:

// ...other page methods

  ionViewDidEnter() {
    const ionSelects = document.querySelectorAll('ion-select');
    ionSelects.forEach((sel) => {
      sel.shadowRoot.querySelectorAll('.select-icon-inner')
        .forEach((elem) => {
          elem.setAttribute('style', 'display: none;');
        });
    });
  }

Based on

此外,就我而言,我使用 slot="end"ion-icon 来放置替换图标:

<ion-item lines="inset">
  <ion-label position="floating">Label</ion-label>

  <ion-select>
    <ion-select-option value="1">Option 1</ion-select-option>
    <ion-select-option value="2">Option 2</ion-select-option>
    <ion-select-option value="2">Option 3</ion-select-option>
  </ion-select>

  <ion-icon color="danger" slot="end" name="arrow-dropdown-circle" class="ion-align-self-center"></ion-icon>
</ion-item>

.select-icon-inner { border-top: transparent!important;}

我认为这只有 ioni3 才有可能。如果只想解决ionic4中的css,则需要知道ionic4

中select-icon的确切class名称

我创建了 this 指令,您可以将其添加到您的 ion-select 中,使其看起来就像其他带占位符(无箭头)的 ion 元素。

用法:

<ion-select placeholder="Choose" appNoArrow>...

要修改图标,调用这个函数

async removeSelectCaret(id){
    const select = await (window.document.querySelector(`#${id}`) as HTMLIonSelectElement).componentOnReady();
    select.shadowRoot.childNodes[1]['style'].display="none";
  }

我找到了使用 css 删除默认 图标 的方法,使用 ::part 指令:

&::part(icon) {
    display: none;
}

然后箭头消失了。

要删除图标,

 ion-select::part(icon) {
    display: none !important;
   }

修改图标,

  ion-select::part(icon) {
    color: #ffa612 !important;
   }

如果你想要相当 ion-select 选项的图标,只需在

上添加 mode="ios"