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

質問編集履歴

4

修正

2016/03/23 15:57

投稿

k499778
k499778

スコア599

title CHANGED
File without changes
body CHANGED
@@ -101,4 +101,98 @@
101
101
 
102
102
  </html>
103
103
 
104
+ ```
105
+
106
+
107
+ ---
108
+
109
+ 追記2
110
+
111
+ 処理1は動く。処理2は想定の動きにならない。
112
+ 「直接書いたコード」と「関数を呼び出した場合」で動きが変わる。
113
+ 文字をremoveできていない。
114
+
115
+ ```HTML
116
+ <!DOCTYPE html>
117
+ <html>
118
+
119
+ <head>
120
+ <meta charset="UTF-8">
121
+ <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
122
+ <style>
123
+ .aaa {
124
+ width: 150px;
125
+ height: 50px;
126
+ background-color: #EFE;
127
+ border: solid 1px #CCC;
128
+ position: absolute;
129
+ }
130
+
131
+ .ddd {
132
+ width: 150px;
133
+ height: 50px;
134
+ background-color: pink;
135
+ border: solid 1px pink;
136
+ position: absolute;
137
+ }
138
+ </style>
139
+ </head>
140
+
141
+ <body>
142
+ <label class="aaa">
143
+ <span class="bbb"></span>
144
+ <input type="hidden" class="ccc">りんご
145
+ </label>
146
+ <br>
147
+ <br>
148
+ <br>
149
+ <label class="aaa">
150
+ <span class="bbb"></span>
151
+ <input type="hidden" class="ccc">いちご
152
+ </label>
153
+ <br>
154
+ <br>
155
+ <br>
156
+ <label class="aaa">
157
+ <span class="bbb"></span>
158
+ <input type="hidden" class="ccc">ぶどう
159
+ </label>
160
+ <br>
161
+ <br>
162
+ <br>
163
+ <label class="ddd">
164
+ <span class="bbb"></span>
165
+ <input type="hidden" class="ccc">りんご
166
+ </label>
167
+ <script>
168
+ $(function() {
169
+ $(".aaa").on("click", function() {
170
+ //処理1。関数に切り出さず、直接書くとエラーにならない
171
+ // this.ownerDocument.querySelector('.ddd').lastChild.data = this.lastChild.data;
172
+ // test(this);
173
+
174
+ //処理2。関数に切り出さず、直接書くとエラーにならない
175
+ // var txt = $(this).text();
176
+ // $(".ddd").contents().filter(function(){ return this.nodeType === this.TEXT_NODE }).remove();
177
+ // $(".ddd").append(txt);
178
+ test2(this);
179
+ });
180
+ });
181
+
182
+ //関数に切り出すとエラーになる
183
+ function test(_this){
184
+ _this.ownerDocument.querySelector('.ddd').lastChild.data = _this.lastChild.data;
185
+ }
186
+
187
+ //関数に切り出すとエラーになる
188
+ function test2(_this){
189
+ var txt = $(_this).text();
190
+ $(".ddd").contents().filter(function(){ return _this.nodeType === _this.TEXT_NODE }).remove(); //ここがうまくいっていない
191
+ $(".ddd").append(txt);
192
+ }
193
+ </script>
194
+ </body>
195
+
196
+ </html>
197
+
104
198
  ```

3

修正

2016/03/23 15:57

投稿

k499778
k499778

スコア599

title CHANGED
File without changes
body CHANGED
@@ -73,13 +73,12 @@
73
73
  <script>
74
74
  $(function() {
75
75
  $(".aaa").on("click", function() {
76
- var txt = $(this).text();
77
76
  //処理1。関数に切り出さず、直接書くとエラーにならない
78
77
  // this.ownerDocument.querySelector('.ddd').lastChild.data = this.lastChild.data;
79
78
  test(this);
80
79
 
81
80
  //処理2。関数に切り出さず、直接書くとエラーにならない
82
- // $(".ddd").text(txt);
81
+ // var txt = $(this).text();
83
82
  // $(".ddd").contents().filter(function(){ return this.nodeType === this.TEXT_NODE }).remove();
84
83
  // $(".ddd").append(txt);
85
84
  test2(this);

2

修正

2016/03/23 15:31

投稿

k499778
k499778

スコア599

title CHANGED
File without changes
body CHANGED
@@ -18,6 +18,7 @@
18
18
  例えばこのような場合です。
19
19
 
20
20
  ```HTML
21
+
21
22
  <!DOCTYPE html>
22
23
  <html>
23
24
 
@@ -73,15 +74,29 @@
73
74
  $(function() {
74
75
  $(".aaa").on("click", function() {
75
76
  var txt = $(this).text();
77
+ //処理1。関数に切り出さず、直接書くとエラーにならない
76
- // this.ownerDocument.querySelector('.ddd').lastChild.data = this.lastChild.data; //関数に切り出さず、直接書くとエラーにならない
78
+ // this.ownerDocument.querySelector('.ddd').lastChild.data = this.lastChild.data;
77
79
  test(this);
80
+
81
+ //処理2。関数に切り出さず、直接書くとエラーにならない
82
+ // $(".ddd").text(txt);
83
+ // $(".ddd").contents().filter(function(){ return this.nodeType === this.TEXT_NODE }).remove();
84
+ // $(".ddd").append(txt);
85
+ test2(this);
78
86
  });
79
87
  });
80
88
 
81
- //thisがエラーになる
89
+ //関数に切り出すとエラーになる
82
90
  function test(this){
83
91
  this.ownerDocument.querySelector('.ddd').lastChild.data = this.lastChild.data;
84
92
  }
93
+
94
+ //関数に切り出すとエラーになる
95
+ function test2(this){
96
+ var txt = $(this).text();
97
+ $(".ddd").contents().filter(function(){ return this.nodeType === this.TEXT_NODE }).remove();
98
+ $(".ddd").append(txt);
99
+ }
85
100
  </script>
86
101
  </body>
87
102
 

1

修正

2016/03/23 15:29

投稿

k499778
k499778

スコア599

title CHANGED
File without changes
body CHANGED
@@ -8,4 +8,83 @@
8
8
  もちろんそのクリックfunction外に使いたい関数を定義し、その引数にもthisと記述しています。
9
9
 
10
10
  このやり方は間違っているのでしょうか?
11
- なぜエラーになったのか教えていただけるとありがたいです。
11
+ なぜエラーになったのか教えていただけるとありがたいです。
12
+
13
+
14
+ ---
15
+
16
+ 追記
17
+
18
+ 例えばこのような場合です。
19
+
20
+ ```HTML
21
+ <!DOCTYPE html>
22
+ <html>
23
+
24
+ <head>
25
+ <meta charset="UTF-8">
26
+ <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
27
+ <style>
28
+ .aaa {
29
+ width: 150px;
30
+ height: 50px;
31
+ background-color: #EFE;
32
+ border: solid 1px #CCC;
33
+ position: absolute;
34
+ }
35
+
36
+ .ddd {
37
+ width: 150px;
38
+ height: 50px;
39
+ background-color: pink;
40
+ border: solid 1px pink;
41
+ position: absolute;
42
+ }
43
+ </style>
44
+ </head>
45
+
46
+ <body>
47
+ <label class="aaa">
48
+ <span class="bbb"></span>
49
+ <input type="hidden" class="ccc">りんご
50
+ </label>
51
+ <br>
52
+ <br>
53
+ <br>
54
+ <label class="aaa">
55
+ <span class="bbb"></span>
56
+ <input type="hidden" class="ccc">いちご
57
+ </label>
58
+ <br>
59
+ <br>
60
+ <br>
61
+ <label class="aaa">
62
+ <span class="bbb"></span>
63
+ <input type="hidden" class="ccc">ぶどう
64
+ </label>
65
+ <br>
66
+ <br>
67
+ <br>
68
+ <label class="ddd">
69
+ <span class="bbb"></span>
70
+ <input type="hidden" class="ccc">りんご
71
+ </label>
72
+ <script>
73
+ $(function() {
74
+ $(".aaa").on("click", function() {
75
+ var txt = $(this).text();
76
+ // this.ownerDocument.querySelector('.ddd').lastChild.data = this.lastChild.data; //関数に切り出さず、直接書くとエラーにならない
77
+ test(this);
78
+ });
79
+ });
80
+
81
+ //thisがエラーになる
82
+ function test(this){
83
+ this.ownerDocument.querySelector('.ddd').lastChild.data = this.lastChild.data;
84
+ }
85
+ </script>
86
+ </body>
87
+
88
+ </html>
89
+
90
+ ```