【问题标题】:Ionic2 select text in ion-searchbarIonic2 在离子搜索栏中选择文本
【发布时间】:2016-04-20 19:21:26
【问题描述】:

在离子输入焦点事件中,我们可以像这样选择所有文本

<ion-input (focus)="selectAll($event)></ion-input>

但在离子搜索栏中它不起作用:

<ion-searchbar (focus)="selectAll($event)></ion-searchbar>

selectAll() 代码是:

public selectAll(event): void {
    event.target.select();
}

我在ionic的Github中看到输入组件返回一个FocusEvent:

inputFocused(event) {
  this.focus.emit(event);
}

另一方面,离子搜索栏返回一个搜索栏(this):

inputFocused() {
  this.focus.emit(this);

  this.isFocused = true;
  this.shouldLeftAlign = true;
  this.setElementLeft();
}

为什么它也不返回 FocusEvent?离子搜索栏聚焦时如何选择所有文本?

谢谢

【问题讨论】:

    标签: ionic-framework ionic2 searchbar


    【解决方案1】:

    离子搜索栏不仅仅是一个输入元素。因此,您需要获取 inputElement 属性而不是目标:

    public selectAll(event): void {
        event.inputElement.select();
    }
    

    【讨论】:

      【解决方案2】:

      ion-searchbar使用:

      <ion-searchbar click="selectAll($event)"></ion-searchbar>
      
      selectAll(event){
         event.target.select();
      }
      

      【讨论】:

        【解决方案3】:

        这可行,但请改用$event.srcElement.select();

        【讨论】:

        • 这可能会回答一个问题。但请多描述一下。
        猜你喜欢
        • 2017-01-02
        • 1970-01-01
        • 2017-08-06
        • 2016-12-25
        • 2017-10-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-21
        • 2018-07-24
        相关资源
        最近更新 更多