子選擇器選擇的是父元素的直接后代嗎
<body>
<div>
<table>
<tr>
<td>123</td>
<td>456</td>
<td>789</td>
<td>111</td>
</tr>
<tr>
<td>gff</td>
<td>fhgf</td>
<td>fre</td>
<td>1gfh</td>
</tr></table>
</div></body>
<script type="text/javascript">
$(function(){
$('table>tr').remove();
})
</script></html>
以上代碼為什么通過(guò)子選擇器選中<table>的直接子元素<tr>然后移除沒(méi)有效果?
2017-09-19
啊哦,我也是剛剛學(xué)完這一節(jié),試了一下,用>好像確實(shí)沒(méi)有移除,但是用$(“table tr”)是可以的,暫時(shí)不知道什么原因,莫非removeClass()對(duì)“>”這種支持?大家知道的話求告知= =不過(guò)上面的代碼貌似還有些其它問(wèn)題哈,我試的是這樣子的:
<!DOCTYPE html>
<html>
<head>
? ?<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
? ?<title></title>
? ?<style>
? ? ? ?.left,
? ? ? ?.right {
? ? ? ? ? ?width: 300px;
? ? ? ? ? ?height: 120px;
? ? ? ?}
? ? ? ?.left div,
? ? ? ?.right div {
? ? ? ? ? ?width: 200px;
? ? ? ? ? ?height: 90px;
? ? ? ? ? ?padding: 5px;
? ? ? ? ? ?margin: 5px;
? ? ? ? ? ?float: left;
? ? ? ?}
? ? ? ?a{
? ? ? ? ? ?font-size: 14px;
? ? ? ? ? ?display:block;
? ? ? ?}
? ? ? ?.newClass{
? ? ? ? ? ? background: #bbffaa;
? ? ? ? }
? ? ? ?.imoocClass{
? ? ? ? ? ?background: red;
? ? ? ?}
? ? ? ?.addBorder{
? ? ? ? ? ?border: 1px solid red;
? ? ? ?}
? ?</style>
? ?<script src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script>
</head>
<body>
<div>
? ?<table>
? ? ? ?<tr class="imoocClass addBorder">
? ? ? ? ? ?<td>123</td>
? ? ? ? ? ?<td>456</td>
? ? ? ? ? ?<td>789</td>
? ? ? ? ? ?<td>111</td>
? ? ? ?</tr>
? ? ? ?<tr class="newClass imoocClass">
? ? ? ? ? ?<td>gff</td>
? ? ? ? ? ?<td>fhgf</td>
? ? ? ? ? ?<td>fre</td>
? ? ? ? ? ?<td>1gfh</td>
? ? ? ?</tr></table>
</div>
<script type="text/javascript">
? ?//$(function(){
? ? ? ?$('table tr').removeClass("imoocClass");//這句可以移除
? ? ? ?//$('table > tr').removeClass("imoocClass");//這句不可以
? // });
</script>
</body>
</html>