`
Rainbow702
  • 浏览: 1080191 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类

JQuery 中 :first 选择器的使用注意点

阅读更多
<!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"分开写,其他类似的选择器也要注意哦!

  • 大小: 2.8 KB
  • 大小: 2.3 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics