<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="jquery-1.7.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
$("#ee").bind("click", function(){
$("div:first").attr("id", "test");
$("#test").append("<button>fffffffffffffffff</button>");
});
});
</script>
</head>
<body>
<button id="ee" >eeeeeeeeee</button>
<div style="background: red;">
<button >ddddddddd</button>
<button >hhhhhhhhh</button>
</div>
</body>
</html>
如果将上面代码中的
$("div:first").attr("id", "test");
更换为
$("div :first").attr("id", "test");
就会发现执行的结果是不一样的,如下面两图:
第一种
第二种
所以,记得不要把"div:first"分开写,其他类似的选择器也要注意哦!