回答編集履歴
2
jQuery\.prototype\.concat \(独自拡張\)
test
CHANGED
@@ -1,4 +1,8 @@
|
|
1
|
+
### Array.prototype.join
|
2
|
+
|
3
|
+
|
4
|
+
|
1
|
-
セレクタを結合する
|
5
|
+
2つ以上のセレクタを結合する。
|
2
6
|
|
3
7
|
|
4
8
|
|
@@ -26,6 +30,38 @@
|
|
26
30
|
|
27
31
|
jQuery(selectors.join()).on('click', handleClick);
|
28
32
|
|
33
|
+
</script>
|
34
|
+
|
35
|
+
```
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
### jQuery.prototype.add
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
2つのjQueryオブジェクトを結合する。
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
```HTML
|
48
|
+
|
49
|
+
<p id="aaa">aaa</p>
|
50
|
+
|
51
|
+
<p id="bbb">bbb</p>
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
<script type="text/javascript">
|
56
|
+
|
57
|
+
'use stirct';
|
58
|
+
|
59
|
+
function handleClick (event) {
|
60
|
+
|
61
|
+
console.log(event.type);
|
62
|
+
|
63
|
+
}
|
64
|
+
|
29
65
|
|
30
66
|
|
31
67
|
var a = jQuery('#aaa'), b = jQuery('#bbb');
|
@@ -38,4 +74,90 @@
|
|
38
74
|
|
39
75
|
|
40
76
|
|
77
|
+
### jQuery.prototype.concat (独自拡張)
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
3つ以上のjQueryオブジェクトを結合する。
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
- [jQuery.prototype.concat - JSFiddle](https://jsfiddle.net/wLfhu8Ls/1/)
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
```HTML
|
90
|
+
|
91
|
+
<p id="aaa">aaa</p>
|
92
|
+
|
93
|
+
<p id="bbb">bbb</p>
|
94
|
+
|
95
|
+
<p id="ccc">ccc</p>
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
<script type="text/javascript">
|
100
|
+
|
101
|
+
'use stirct';
|
102
|
+
|
103
|
+
function handleClick (event) {
|
104
|
+
|
105
|
+
console.log(event.type);
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
console.log(jQuery.prototype.concat);
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
Object.defineProperty(jQuery.prototype, 'concat', {
|
116
|
+
|
117
|
+
writable: true,
|
118
|
+
|
119
|
+
enumerable: false,
|
120
|
+
|
121
|
+
configurable: true,
|
122
|
+
|
123
|
+
value: function concat (elements) {
|
124
|
+
|
125
|
+
var clones = jQuery(this),
|
126
|
+
|
127
|
+
push = this.push;
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
for (var i = 0, l = arguments.length; i < l; ++i) {
|
132
|
+
|
133
|
+
push.apply(clones, arguments[i]);
|
134
|
+
|
135
|
+
}
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
return clones;
|
140
|
+
|
141
|
+
}
|
142
|
+
|
143
|
+
});
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
var a = jQuery('#aaa'), b = jQuery('#bbb'), c = jQuery('#ccc');
|
148
|
+
|
149
|
+
jQuery(a.concat(b, c)).on('click', handleClick);
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
console.dir(a.concat(b, c));
|
154
|
+
|
155
|
+
console.dir(a);
|
156
|
+
|
157
|
+
</script>
|
158
|
+
|
159
|
+
```
|
160
|
+
|
161
|
+
|
162
|
+
|
41
163
|
Re: k499778 さん
|
1
セレクタ用変数の変数名を変更
test
CHANGED
@@ -22,9 +22,9 @@
|
|
22
22
|
|
23
23
|
|
24
24
|
|
25
|
-
var
|
25
|
+
var selectors = ['#aaa', '#bbb'];
|
26
26
|
|
27
|
-
jQuery(
|
27
|
+
jQuery(selectors.join()).on('click', handleClick);
|
28
28
|
|
29
29
|
|
30
30
|
|