博客列表 >实例演示$()函数的四个参数,联系将课堂上提及的所有getter/setter方法

实例演示$()函数的四个参数,联系将课堂上提及的所有getter/setter方法

后网络时代
后网络时代原创
2021年01月13日 15:15:53514浏览

$()函数的四个参数

1. $(选择器,上下文): 获取元素

jQuery([selector,[context]])

  1. <!DOCTYPE html>
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
  9. </head>
  10. <body>
  11. 中国一定强
  12. <div class="title"><h2>h2选择器</h2></div>
  13. <script>
  14. $("body").css('color','cyan');
  15. $('body').css("backgroundColor",'yellow');
  16. $(".title h2").css('color','red');
  17. $(".title h2").css('backgroundColor','green');
  18. </script>
  19. </body>
  20. </html>
2. $(js对象)
  1. <!DOCTYPE html>
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
  9. </head>
  10. <body>
  11. 中国一定强
  12. <div class="title"><h2>h2选择器</h2></div>
  13. <script>
  14. $(document.body).css('color','cyan');
  15. $(document.body).css('backgroundColor','red');
  16. $(document.querySelector('.title h2')).css('backgroundColor','darkgreen');
  17. $(document.querySelector('.title h2')).css('color','#000');
  18. $(document.querySelectorAll('.title h2')).css('color','cyan');
  19. </script>
  20. </body>
  21. </html>
3.html标签字符
  1. <!DOCTYPE html>
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
  9. </head>
  10. <body>
  11. 中国一定强
  12. <div class="title"><h2>h2选择器</h2></div>
  13. <script>
  14. $("<span>人都爱美丽</span>").appendTo(document.querySelector(".title h2"));
  15. </script>
  16. </body>
  17. </html>
4.回调函数

$(function(){
// 文档就绪
});// $(document).ready() 的简写;当DOM加载完成后,执行其中的函数。

  1. <!DOCTYPE html>
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
  9. </head>
  10. <body>
  11. 中国一定强
  12. <div class="title"><h2>h2选择器</h2></div>
  13. <script>
  14. $(function(){
  15. alert('加载完成');
  16. })
  17. </script>
  18. </body>
  19. </html>

联系将课堂上提及的所有getter/setter方法

  1. <!DOCTYPE html>
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
  9. </head>
  10. <body>
  11. <ul>
  12. <li>item1</li>
  13. <li>item2</li>
  14. <li>item3</li>
  15. <li>item4</li>
  16. <li>item5</li>
  17. </ul>
  18. <h2></h2>
  19. <form action="">
  20. <input type="text" name="user" value="输入用户名">
  21. <input type="password" name="password" value="123456">
  22. 保密:<input type="radio" name="xinxi" value='保密' checked>公开:<input type="radio" name="xinxi" value='公开'>
  23. <button>提交</button>
  24. <p></p>
  25. </form>
  26. <script>
  27. $("body").css('color','cyan');
  28. $('<span>'+$("body").css('color')+'</span>').appendTo($('h2'));
  29. $('h2').attr('style',"color:red;display:block;width:300px;background-color:cyan");
  30. $('h2').html();
  31. $('h2').html("<p>通过html添加</p>");
  32. $('h2').text();
  33. $('h2').text('text设置文本').css('color',"blue").css('backgroundColor',"yellow");
  34. $('form').attr('action',function(){ return 'index.php?id=11'; })
  35. $('form')[0]===$('form').get(0);
  36. $('form input[name=user]').val('设置的用户名').css('color','red');
  37. $('input[name=user]').val('设置的用户名').css('color','red');
  38. $('form input[name="password"]').css('color','green').val();
  39. $('form input[type="radio"]').attr('checked','');
  40. $('form input[type="radio"]')[0].checked
  41. $('form input[type="radio"]')[1].checked
  42. </script>
  43. </body>
  44. </html>

效果:

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议