.add( selector )
引数
- (String)selector
- 設定するclass属性の値
対象のjQueryオブジェクトに引数のDOM要素を追加した、新たなjQueryオブジェクトを返す。add()した後に実行したメソッドは
全てのDOM要素に実行される。
サンプル
Javascript
$('.jqTest1 :button').on('click', function(e){
$('.jqTest1 div').css({
width: 100,
height: 100,
background: '#555555'
}).add('.jqTest1 p').css({
border: '5px solid #990000'
});
});
HTML
<div class="jqTest1">
<div> </div>
<p>test</p>
<button>execute</button>
</div>
新たなオブジェクトを返す。よってこれは違う結果になる。
サンプル
Javascript
$('.jqTest2 .execute1').on('click', function(e){
var div$ = $('<div>').css({
width: 100,
height: 100,
background: '#555555'
})
.add('<p>', {text: '追加される'}).css({
border: '5px solid #990000'
});
$('.jqTest2').append(div$);
});
$('.jqTest2 .execute2').on('click', function(e){
var div$ = $('<div>').css({
width: 100,
height: 100,
background: '#555555'
});
div$.add('<p>', {text: '追加されない'}).css({
border: '5px solid #990000'
});
$('.jqTest2').append(div$);
});
HTML
<div class="jqTest2">
<button class="execute1">execute1</button><button class="execute2">execute2</button>
<hr />
</div>
正しくは
サンプル
Javascript
$('.jqTest3 .execute1').on('click', function(e){
var div$ = $('<div>').css({
width: 100,
height: 100,
background: '#555555'
});
div$ = div$.add('<p>', {text: '追加される'}).css({
border: '5px solid #990000'
});
$('.jqTest3').append(div$);
});
$('.jqTest3 .execute2').on('click', function(e){
var div$ = $('<div>').css({
width: 100,
height: 100,
background: '#555555'
});
$('.jqTest3').append(div$.add('<p>', {text: '追加される'})
.css({ border: '5px solid #990000'})
);
});
HTML
<div class="jqTest3">
<button class="execute1">execute1</button><button class="execute2">execute2</button>
<hr />
</div>
となる。
.add( elements )
引数
- (String)selector
- 設定するclass属性の値
.add( html )
引数
- (String)selector
- 設定するclass属性の値
.add( jQuery object )
引数
- (String)selector
- 設定するclass属性の値
.add( selector, context )
引数
- (String)selector
- 設定するclass属性の値