4.1 this触发事件的对象

this在 JS 中表示触发事件的对象
在jquery 中有三个 是不需要加双引号的

  1. $(this)
  2. $(document)
  3. $(window)
  4. //点击某个P,让这个p改变背景色
  5. $("p").click(function(){
  6. $(this).css("backgroundColor","red")
  7. })

4.2 parent()父节点

parent(): 父节点 , 只会选中亲父亲不会选中爷爷或者曾爷爷

  1. //点击那个p让它的父节点背景色改变
  2. $("p").click(function(){
  3. $(this).parent().css("backgroundColor","blue");
  4. })

4.3 siblings()兄弟节点

siblings() : 兄弟节点

  1. //siblings() : 兄弟节点
  2. $("p").click(function(){
  3. $(this).siblings().css("backgroundColor","red");
  4. })

4.4 children()儿子节点

  1. children 儿子节点
  2. //children 儿子节点
  3. $("div").click(function(){
  4. $(this).children().css("backgroundColor","red");
  5. })

4.5不常用的节点操作

next() : 选中下一个兄弟节点
nextAll() : 后面所有的兄弟节点
prev() : 选中上一个兄弟
prevAll() : 前面所有的兄弟
parents() : 祖先节点, 也可以传递参数指定
find() : 会选中所有后代节点 , 可以传递参数指定节点

  1. //点击某个P,让这个p改变背景色
  2. $("p").click(function(){
  3. $(this).css("backgroundColor","red")
  4. })
  5. // //点击那个p让它的父节点背景色改变
  6. $("p").click(function(){
  7. $(this).parent().css("backgroundColor","blue");
  8. })
  9. //siblings() : 兄弟节点
  10. $("p").click(function(){
  11. $(this).siblings().css("backgroundColor","red");
  12. })
  13. //children 儿子节点
  14. $("div").click(function(){
  15. $(this).children().css("backgroundColor","red");
  16. })
  17. //next() : 选中下一个兄弟节点
  18. $("p").click(function(){
  19. $(this).nextAll().css("backgroundColor","red");
  20. })
  21. //nextAll() 后面所有的兄弟节点
  22. $("p").click(function(){
  23. $(this).nextAll().css("backgroundColor","red");
  24. })
  25. //前一个兄弟
  26. $("p").click(function(){
  27. $(this).prev().css("backgroundColor","red");
  28. })
  29. //前面所有的节点
  30. $("p").click(function(){
  31. $(this).prevAll().css("backgroundColor","red");
  32. })
  33. //所有的祖先节点
  34. $("p").click(function(){
  35. $(this).parents().css("backgroundColor","red");
  36. })
  37. //find() : 会选中所有后代节点 , 可以传递参数指定节点
  38. $("#body").click(function(){
  39. $(this).find("div").css("backgroundColor","red");
  40. })

连续打点

  1. //连续打点
  2. $("p").click(function(){
  3. $(this).css("backgroundColor","red")
  4. .siblings().css("backgroundColor","green")
  5. .parent().css("backgroundColor","purple")
  6. .siblings().css("backgroundColor","yellow");
  7. });

分条打点

  1. //分条书写
  2. $("p").click(function(){
  3. $(this).css("backgroundColor","red")
  4. $(this).siblings().css("backgroundColor","green")
  5. $(this).parent().css("backgroundColor","purple")
  6. $(this).parent().siblings().css("backgroundColor","yellow")
  7. });