我在HTML的代碼:jquery的每個()和js陣列未按預期
<div class="box-content">
<span>
<h4 id="title">title1</h4>
<h5 id="texto"></h5>
</span>
<span>
<h4 id="title">title2</h4>
<h5 id="texto"></h5>
</span>
<span>
<h4 id="title">title3</h4>
<h5 id="texto"></h5>
</span>
....
</div>
,我需要使用陣列如下
$(".pluscontrol").click(function(){
var itemBoxValues1 = ["text1", "text2", "text3"];
var i = 0;
$('.box-content').children().each(function(){
var tmp = itemBoxValues1[i];
$('.box-content').children().children("h5").text(" id: "+i+" val "+tmp);
i++;
});
});
來填充結構,但它不「科技工作如我所料,因爲在所有<h5>
元素打印:
<span>
<h4 id="title">title n </h4>
<h5 id="texto">id: 35 val 23</h5>
</span>
我不明白爲什麼它的發生。謝謝。
您目前使用的每個內的孩子們選擇。你每個函數內的'this'將是你正在迭代的當前對象。減少'$('。box-content')。children()。children(「h5」)...''到'$(this).children('h5')...' – Jecoms
'id''根據定義應該是唯一的。 – PeterKA
不相關,但我很好奇,爲什麼自己增加價值'我'?你可以做'.each(function(index,element){})' –