我有以下代码:
html
<div id="Test" class="test">test</div>
<div id="Test1" class="test">test</div>
<div data-id="Test2" class="test">test</div>
<br />
<div id="result1"></div>
<div id="result2"></div>
jQuery
var result1 = 'Result1:',
result2 = 'Result2:';
$('.test').each(function () {
var test = $(this),
testId1 = (typeof this.id !== "undefined" ? this.id : test.data('id')),
testId2 = (typeof this.id !== "undefined" ? this.id : '');
if (testId2 == '') {
testId2 = test.data('id');
}
result1 += testId1 + '.';
result2 += testId2 + '.';
});
$('#result1').html(result1);
$('#result2').html(result2);
对于两个结果 div,我希望它们的内容是Result1:Test.Test1.Test2.
和Result2:Test.Test1.Test2.
然而,第一个结果 div 的内容为Result1:Test.Test1..
.
为什么是这样?