jquery HTML 関数を使用して div 内の HTML コンテンツを更新しようとしています。
<div id="test"></div>
.
.
.
<div id = "test" style="display:none"></div>'
コンテンツを更新しているとき。
(#test).html(result)
両方の div を更新する必要があります。どうすればこれを達成できますか。
ありがとう
ID は一意である必要があります。代わりにクラスを使用する必要があります
<div class="test" id="test1"></div>
<div class="test" id="test2"></div>
$('div.test')
.html('<p>All new content.You want!</p>');
Id
で一意である必要がありますHTML
。代わりにクラスを使用する必要があります
HTML:
<div class="test"></div>
.
.
.
<div class= "test" style="display:none"></div>'
JavaScript:
$(".test").html(result)
あなたのhtmlコードは次のとおりだと思います:
<div id="#test">xxxx</div><div id="test">cccc</div>
$("#test"); を呼び出すと、これらは長さ 2 の配列を返します。そのため、すべてを更新するには各配列を呼び出す必要がありました。#test の両方に値 'ddd' を更新すると仮定します。
for(i=0;i<$('#test').length;i++){
$('#test')[i].html('ddd');
}
IDの代わりにクラスを使用して、これを試してください
$('.test').html(result)