為什么最后一題a:first返回了a的3個內容?求詳細解答,多謝
這里 <div class="left first-div">
? ? ? ?<div class="div">
? ? ? ? ? ?<a>:first-child</a>
? ? ? ? ? ?<a>第二個元素</a>
? ? ? ? ? ?<a>:last-child</a>
? ? ? ?</div> ?
$(".left a:first").text(function(idnex,text){
? ? ? ? ? ?return '增加新的文本內容' + text
? ? ? ?})
這個為啥還會返回a(1),a(2)的內容???a:first不是代表a的第一個嗎?
結果如下:
增加新的文本內容:first-child
第二個元素
替換第一個a元素的內容
這里 <div class="left first-div">
? ? ? ?<div class="div">
? ? ? ? ? ?<a>:first-child</a>
? ? ? ? ? ?<a>第二個元素</a>
? ? ? ? ? ?<a>:last-child</a>
? ? ? ?</div> ?
$(".left a:first").text(function(idnex,text){
? ? ? ? ? ?return '增加新的文本內容' + text
? ? ? ?})
這個為啥還會返回a(1),a(2)的內容?。縜:first不是代表a的第一個嗎?
結果如下:
增加新的文本內容:first-child
第二個元素
替換第一個a元素的內容
這里 <div class="left first-div">
? ? ? ?<div class="div">
? ? ? ? ? ?<a>:first-child</a>
? ? ? ? ? ?<a>第二個元素</a>
? ? ? ? ? ?<a>:last-child</a>
? ? ? ?</div> ?
$(".left a:first").text(function(idnex,text){
? ? ? ? ? ?return '增加新的文本內容' + text
? ? ? ?})
這個為啥還會返回a(1),a(2)的內容?。縜:first不是代表a的第一個嗎?
結果如下:
增加新的文本內容:first-child
第二個元素
替換第一個a元素的內容
2017-11-07
a:first確實是代表第一個a,你的誤區(qū)是三個a都顯示再一個div中,你把它們統(tǒng)一看成了第一個a的值,建議你給第一個加一個背景,這樣不管內容怎么變你就知道哪個才是第一個a的值:
<h3>.html()與.text()</h3>
? ? <div class="left first-div">
? ? ? ? <div class="div">
? ? ? ? ? ? <a class="firsta">:first-child</a>
? ? ? ? ? ? <a>第二個元素</a>
? ? ? ? ? ? <a>:last-child</a>
? ? ? ? </div> ?
? ? ? ? <div class="div">
? ? ? ? ? ? <a>:first-child</a>
? ? ? ? ? ? <a>第二個元素</a>
? ? ? ? ? ? <a>:last-child</a>
? ? ? ? </div>
? ? </div> ? ?
.firsta{ background-color: red;}不知道這樣解釋你明不明白