remove()為什么不能刪除第一個子元素?
代碼如下,我把原代碼的“:contains('3')”換成了$(".test2")remove(“:first-child”)就無法實現(xiàn)了,類似的還有幾個選擇器,用了也無法實現(xiàn)
<script type="text/javascript">
? ? $("button:first").on('click', function() {
? ? ? ? //刪除整個 class=test1的div節(jié)點
? ? ? ? $(".test1").remove()
? ? })
? ? $("button:last").on('click', function() {
? ? ? ? //找到所有p元素中,包含了3的元素
? ? ? ? //這個也是一個過濾器的處理
? ? ? ? $(".test2").remove(":first-child")
? ? })
? ? </script>
2016-08-14
因為 remove() 是會移除自身的,你使用?:first-child 這個過濾器沒有意義,所以估計沒有這種語法吧。
你可以使用 :first 這個過濾器試下,意味著移除?$(".test2") 中的第一個,這個可以通過測試。
2016-10-20
$(".test2 p")remove(“:first-child”)這樣寫,倒是可以刪除掉class="test2"下面的第一個p元素。
2016-08-14
不知道為什么,但是試試是要是限定條件在前面的話就沒有問題,比如$(".test2 p:first-child").remove( )