Array.prototype.join
2つ以上のセレクタを結合する。
HTML
1<p id="aaa">aaa</p>
2<p id="bbb">bbb</p>
3
4<script type="text/javascript">
5'use stirct';
6function handleClick (event) {
7 console.log(event.type);
8}
9
10var selectors = ['#aaa', '#bbb'];
11jQuery(selectors.join()).on('click', handleClick);
12</script>
jQuery.prototype.add
2つのjQueryオブジェクトを結合する。
HTML
1<p id="aaa">aaa</p>
2<p id="bbb">bbb</p>
3
4<script type="text/javascript">
5'use stirct';
6function handleClick (event) {
7 console.log(event.type);
8}
9
10var a = jQuery('#aaa'), b = jQuery('#bbb');
11jQuery(a.add(b)).on('click', handleClick);
12</script>
jQuery.prototype.concat (独自拡張)
3つ以上のjQueryオブジェクトを結合する。
HTML
1<p id="aaa">aaa</p>
2<p id="bbb">bbb</p>
3<p id="ccc">ccc</p>
4
5<script type="text/javascript">
6'use stirct';
7function handleClick (event) {
8 console.log(event.type);
9}
10
11console.log(jQuery.prototype.concat);
12
13Object.defineProperty(jQuery.prototype, 'concat', {
14 writable: true,
15 enumerable: false,
16 configurable: true,
17 value: function concat (elements) {
18 var clones = jQuery(this),
19 push = this.push;
20
21 for (var i = 0, l = arguments.length; i < l; ++i) {
22 push.apply(clones, arguments[i]);
23 }
24
25 return clones;
26 }
27});
28
29var a = jQuery('#aaa'), b = jQuery('#bbb'), c = jQuery('#ccc');
30jQuery(a.concat(b, c)).on('click', handleClick);
31
32console.dir(a.concat(b, c));
33console.dir(a);
34</script>
Re: k499778 さん
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/06/01 14:58