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

来源:互联网 发布:抽签用的软件 编辑:程序博客网 时间:2024/05/07 00:52
<!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
  • 查看图片附件
0 0
原创粉丝点击