回答編集履歴

2

ちょうせい

2021/04/22 04:54

投稿

yambejp
yambejp

スコア117755

test CHANGED
@@ -38,6 +38,8 @@
38
38
 
39
39
  if(e.keyCode == 13){
40
40
 
41
+ e.preventDefault();
42
+
41
43
  t.value=v.substring(0,s)+"\n"+v.substring(s);
42
44
 
43
45
  t.selectionStart=s+1;

1

追加

2021/04/22 04:54

投稿

yambejp
yambejp

スコア117755

test CHANGED
@@ -17,3 +17,37 @@
17
17
  ```
18
18
 
19
19
  ただしformにイベントリスナーを直接書くのはおすすめしないのでjsを分離したほうがよいでしょう
20
+
21
+
22
+
23
+ # 追加処理
24
+
25
+ ```javascript
26
+
27
+ document.addEventListener('keydown',e=>{
28
+
29
+ const selection = getSelection();
30
+
31
+ const t=e.target;
32
+
33
+ if(t.closest('textarea')){
34
+
35
+ const v=t.value;
36
+
37
+ const s=t.selectionStart;
38
+
39
+ if(e.keyCode == 13){
40
+
41
+ t.value=v.substring(0,s)+"\n"+v.substring(s);
42
+
43
+ t.selectionStart=s+1;
44
+
45
+ t.selectionEnd=s+1;
46
+
47
+ }
48
+
49
+ }
50
+
51
+ });
52
+
53
+ ```