現在開発をしていてjqueryで
clickイベントの処理を行っていました。
そのクリックfunction内ではthisを使って、
クリックしたセレクタを指定することはできたのですが、
そのクリックfunction内にfunction(関数)を書き、その引数にthisを渡すとエラーになりました。
もちろんそのクリックfunction外に使いたい関数を定義し、その引数にもthisと記述しています。
このやり方は間違っているのでしょうか?
なぜエラーになったのか教えていただけるとありがたいです。
追記
例えばこのような場合です。
HTML
1 2<!DOCTYPE html> 3<html> 4 5<head> 6 <meta charset="UTF-8"> 7 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 8 <style> 9 .aaa { 10 width: 150px; 11 height: 50px; 12 background-color: #EFE; 13 border: solid 1px #CCC; 14 position: absolute; 15 } 16 17 .ddd { 18 width: 150px; 19 height: 50px; 20 background-color: pink; 21 border: solid 1px pink; 22 position: absolute; 23 } 24 </style> 25</head> 26 27<body> 28 <label class="aaa"> 29 <span class="bbb"></span> 30 <input type="hidden" class="ccc">りんご 31 </label> 32 <br> 33 <br> 34 <br> 35 <label class="aaa"> 36 <span class="bbb"></span> 37 <input type="hidden" class="ccc">いちご 38 </label> 39 <br> 40 <br> 41 <br> 42 <label class="aaa"> 43 <span class="bbb"></span> 44 <input type="hidden" class="ccc">ぶどう 45 </label> 46 <br> 47 <br> 48 <br> 49 <label class="ddd"> 50 <span class="bbb"></span> 51 <input type="hidden" class="ccc">りんご 52 </label> 53 <script> 54 $(function() { 55 $(".aaa").on("click", function() { 56 //処理1。関数に切り出さず、直接書くとエラーにならない 57 // this.ownerDocument.querySelector('.ddd').lastChild.data = this.lastChild.data; 58 test(this); 59 60 //処理2。関数に切り出さず、直接書くとエラーにならない 61 // var txt = $(this).text(); 62 // $(".ddd").contents().filter(function(){ return this.nodeType === this.TEXT_NODE }).remove(); 63 // $(".ddd").append(txt); 64 test2(this); 65 }); 66 }); 67 68 //関数に切り出すとエラーになる 69 function test(this){ 70 this.ownerDocument.querySelector('.ddd').lastChild.data = this.lastChild.data; 71 } 72 73 //関数に切り出すとエラーになる 74 function test2(this){ 75 var txt = $(this).text(); 76 $(".ddd").contents().filter(function(){ return this.nodeType === this.TEXT_NODE }).remove(); 77 $(".ddd").append(txt); 78 } 79 </script> 80</body> 81 82</html> 83
追記2
処理1は動く。処理2は想定の動きにならない。
「直接書いたコード」と「関数を呼び出した場合」で動きが変わる。
文字をremoveできていない。
HTML
1<!DOCTYPE html> 2<html> 3 4<head> 5 <meta charset="UTF-8"> 6 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 7 <style> 8 .aaa { 9 width: 150px; 10 height: 50px; 11 background-color: #EFE; 12 border: solid 1px #CCC; 13 position: absolute; 14 } 15 16 .ddd { 17 width: 150px; 18 height: 50px; 19 background-color: pink; 20 border: solid 1px pink; 21 position: absolute; 22 } 23 </style> 24</head> 25 26<body> 27 <label class="aaa"> 28 <span class="bbb"></span> 29 <input type="hidden" class="ccc">りんご 30 </label> 31 <br> 32 <br> 33 <br> 34 <label class="aaa"> 35 <span class="bbb"></span> 36 <input type="hidden" class="ccc">いちご 37 </label> 38 <br> 39 <br> 40 <br> 41 <label class="aaa"> 42 <span class="bbb"></span> 43 <input type="hidden" class="ccc">ぶどう 44 </label> 45 <br> 46 <br> 47 <br> 48 <label class="ddd"> 49 <span class="bbb"></span> 50 <input type="hidden" class="ccc">りんご 51 </label> 52 <script> 53 $(function() { 54 $(".aaa").on("click", function() { 55 //処理1。関数に切り出さず、直接書くとエラーにならない 56 // this.ownerDocument.querySelector('.ddd').lastChild.data = this.lastChild.data; 57 // test(this); 58 59 //処理2。関数に切り出さず、直接書くとエラーにならない 60 // var txt = $(this).text(); 61 // $(".ddd").contents().filter(function(){ return this.nodeType === this.TEXT_NODE }).remove(); 62 // $(".ddd").append(txt); 63 test2(this); 64 }); 65 }); 66 67 //関数に切り出すとエラーになる 68 function test(_this){ 69 _this.ownerDocument.querySelector('.ddd').lastChild.data = _this.lastChild.data; 70 } 71 72 //関数に切り出すとエラーになる 73 function test2(_this){ 74 var txt = $(_this).text(); 75 $(".ddd").contents().filter(function(){ return _this.nodeType === _this.TEXT_NODE }).remove(); //ここがうまくいっていない 76 $(".ddd").append(txt); 77 } 78 </script> 79</body> 80 81</html> 82

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/03/23 15:53
2016/03/23 16:03 編集
2016/03/23 22:49 編集
2016/03/24 14:33
2016/03/24 23:16 編集
2016/03/26 03:47