我不明白爲什麼代碼的最後一部分會影響第二個div
而不是第一個像第一個那樣。另外,那裏的所有其他內容都消失了,這不是理想的行爲。我正在閱讀的書沒有解釋爲什麼第二個div受到影響,而不是第一個。爲什麼這發生在第二個而不是第一個?我認爲div:last-child
會改變第一個div
,因爲它被認爲是直系父母。與子元素的jQuery問題
jQuery的
$(document).ready(function(){
$('div:first-child').text('This is the first child');
$('div:last-child').text('I\'ve changed this!');
});
HTML
<div id=」myfirstdiv」>
<strong class=」changemytext」>some name</strong>
<p class=」changemytext」>Some text<p>
<strong>another name</strong>
<p>More text<p>
</div>
<div id=」myseconddiv」>
<strong class=」changemytext」>some name</strong>
<p class=」changemytext」>Some text<p>
<strong>another name</strong>
<p>More text<p>
</div>
</body>
什麼你想在這裏做什麼? –
我深信,只有那些元素會受到影響,正如我在上面所說的那樣,但是,第一個父母的第一個元素髮生了變化,第二個元素的變化也是如此,但其餘的內容也都消失了。我認爲第一個div的第一個和最後一個元素只會受到影響。 –
感謝科迪簡化我的代碼。 –