teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

3

codeSandboxのサンプルプロジェクトを作成

2019/08/15 10:07

投稿

KuwabataK
KuwabataK

スコア306

answer CHANGED
@@ -10,4 +10,14 @@
10
10
  this.$refs.r.focus();
11
11
  this.$refs.r.setSelectionRange(select_len + 1, select_len + 1);
12
12
  });
13
- ```
13
+ ```
14
+
15
+ #### 2019/08/15 追記
16
+
17
+ 以下にVueのプロジェクトを作って同じコードを試してみましたが、この環境だと `setSelectionRange`
18
+ はうまく動いてくれるようです。(Vuetifyは使っていませんが)
19
+
20
+ 変更点は`select_len`をidではなく`$ref`を使って取って来るようにしたぐらいですね。
21
+ 後は、関係ないですが`var`を`const`にしたりとか・・・
22
+
23
+ [https://codesandbox.io/s/vue-template-oszsy?fontsize=14](https://codesandbox.io/s/vue-template-oszsy?fontsize=14)

2

delete

2019/08/15 10:06

投稿

KuwabataK
KuwabataK

スコア306

answer CHANGED
@@ -10,13 +10,4 @@
10
10
  this.$refs.r.focus();
11
11
  this.$refs.r.setSelectionRange(select_len + 1, select_len + 1);
12
12
  });
13
- ```
14
-
15
- また、`this.$nextTick()` は`Promise`を返すので、`await`を使って以下のように書けます。
16
- 個人的にはこちらのほうが可読性が高くなるのでおすすめです
17
-
18
- ```js
19
- await this.$nextTick()
20
- this.$refs.r.focus();
21
- this.$refs.r.setSelectionRange(select_len + 1, select_len + 1);
22
13
  ```

1

awaitバージョンを追加

2019/08/14 11:58

投稿

KuwabataK
KuwabataK

スコア306

answer CHANGED
@@ -10,4 +10,13 @@
10
10
  this.$refs.r.focus();
11
11
  this.$refs.r.setSelectionRange(select_len + 1, select_len + 1);
12
12
  });
13
+ ```
14
+
15
+ また、`this.$nextTick()` は`Promise`を返すので、`await`を使って以下のように書けます。
16
+ 個人的にはこちらのほうが可読性が高くなるのでおすすめです
17
+
18
+ ```js
19
+ await this.$nextTick()
20
+ this.$refs.r.focus();
21
+ this.$refs.r.setSelectionRange(select_len + 1, select_len + 1);
13
22
  ```