你好 - 我的问题最好概括为预期输出和实际输出。 任何线索表明为什么在使用以下HTML和JS代码时会出现这种情况?
HTML代码:
<h3>CATEGORY 1</h3>
<p>Item 1</p>
<p>Item 2</p>
<h3>CATEGORY 2</h3>
<p>Item 3</p>
<p>Item 4</p>
<h3>CATEGORY 3</h3>
<p>Item 5</p>
<p>Item 6</p>
JavaScript / jQuery 代码:
$(".h3").each(function () {
// Display H3 Text
console.log($(this).text());
$(this).siblings('p').each(function () {
if ( $(this).next().is('h3') ) {
// Display Last Paragraph Text Before <H3>
console.log($(this).text());
// Break the Each Loop, Go to next H3
return false;
}
else {
// Display Paragraph Text
console.log($(this).text());
}
});
});
预期输出:
CATEGORY 1
Item 1
Item 2
CATEGORY 2
Item 3
Item 4
CATEGORY 3
Item 5
Item 6
实际(意外)输出:
CATEGORY 1
Item 1
Item 2
CATEGORY 2
Item 1
Item 2
CATEGORY 3
Item 1
Item 2
谢谢。
p
元素不在h3
元素内。它们甚至不被允许在那里。 - Felix Kling