【问题标题】:Select all the text on tap of the ion-input选择离子输入上的所有文本
【发布时间】:2021-07-26 16:50:54
【问题描述】:

如何选择离子输入中的所有文本。

下面是我尝试选择文本框中所有文本的方法。

轨迹1:

<ion-input (tap)="selectAll($event)" type="text" />

selectAll(event){
  event.target.select();
  //window.document.element.select()
}

Trail2:

<ion-input (tap)="this.select()" type="text" />

Trail3:

<ion-input (tap)="this.setSelectionRange(0,value.length)" type="text" />

Trail4:

  <ion-input #textbox (tap)="selectAll()" type="text" />

@ViewChild('textbox ') textbox ;
this.textbox .nativeElement.select();

没有什么对我有用。

如果有其他试用版,请告诉我

【问题讨论】:

  • 你为什么不能尝试专注而不是点击?

标签: javascript html angular select ionic4


【解决方案1】:

我稍微改变了你的第一次试验,这似乎有效。 Ion-Input 是一个 html 输入元素的包装器。要选择里面的所有文本,您需要区分两种情况:直接点击/点击内部 html 输入,然后您可以直接在目标上使用select(),或者您点击/点击外部离子输入包装器,那么您需要先访问子输入。像这样的。

 <ion-input (tap)="select($event)"></ion-input>

 select(event: any) {
   if (event.target.select) { // tapped directly on html input
     event.target.select();
   } else if (event.target.children[0].select) { // tapped on the wrapper
     event.target.children[0].select();
   }
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-26
    • 2017-03-20
    • 2012-05-12
    • 2011-05-03
    • 1970-01-01
    • 2018-12-05
    • 2018-01-08
    • 2019-10-06
    相关资源
    最近更新 更多