jQuery 多种基础页面属性

末蓝、 2022-07-16 09:00 282阅读 0赞

完成效果:学习jQuery,了解其基础属性,并能完成与Html代码相同的功能。

1.基本语法

$(selector).action()

  • $ 符合定义这是一个jQuery语句
  • (selector) 用来选择某个HTML元素,其语法和CSS的selector语法一样。
  • action() 定义操作该HTML元素的方法。

比如:

$(this).hide() – 隐藏当前元素.

$(“p”).hide() – 隐藏所以

元素.

$(“.test”).hide() -隐藏所以类名为test的元素.

$(“#test”).hide() - 隐藏ID为test的元素。

文档准备好代码:

  1. $(document).ready(function(){
  2. // jQuery methods go here...
  3. });

简化代码:

  1. $(function(){
  2. // jQuery methods go here...
  3. });

2.id选择器:

代码:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <script src="../jquery/jquery-1.4.2.min.js"></script>
  6. <script>
  7. $(document).ready(function(){
  8. $("button").click(function(){
  9. $("#test").hide();
  10. });
  11. });
  12. </script>
  13. </head>
  14. <body>
  15. <h2>这是头部</h2>
  16. <p>这是段落</p>
  17. <p id="test">这是另一个段落</p>
  18. <button>点击我</button>
  19. </body>
  20. </html>

效果:点击按钮后,只有id为test的段落消失。

CenterCenter 1

3.Class选择器:

代码:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <script src="../jquery/jquery-1.4.2.min.js"></script>
  6. <script>
  7. $(document).ready(function(){
  8. $("button").click(function(){
  9. $(".test").hide();
  10. });
  11. });
  12. </script>
  13. </head>
  14. <body>
  15. <h2 class="test">这是头部</h2>
  16. <p class="test">这是段落</p>
  17. <p >这是另一个段落</p>
  18. <button>点击我</button>
  19. </body>
  20. </html>

效果:点击按钮后,所有类名称为test的属性隐藏
Center 2

3.单击隐藏:

代码:

  1. $("p").click(function(){
  2. $(this).hide();
  3. });

4.双击隐藏:

代码:

  1. $("p").dblclick(function(){
  2. $(this).hide();
  3. <strong><span style="font-size:18px;">}); </span></strong>

5.获得焦点

  1. $("input").focus(function(){
  2. $(this).css("background-color","#cccccc");
  3. });

6.失去焦点

  1. $("input").blur(function(){
  2. $(this).css("background-color","#ffffff");
  3. });

7.鼠标进入图片和离开图片事件

代码:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <script src="jquery-1.10.2.min.js"></script>
  7. <script>
  8. $(function () {
  9. $("img").hover(function () {
  10. alert("鼠标移动到图片上");
  11. },function () {
  12. alert("鼠标从图片上移出");
  13. })
  14. });
  15. </script>
  16. </head>
  17. <body>
  18. <img src="../image/1.jpg" id="img">
  19. </body>

效果:

Center 3

**Center 4

8.在div中增加新的文字,并设置新增加的点击后仍可增加新的。**

代码:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <script src="jquery-1.4.2.min.js"></script>
  7. <style>
  8. .clickme{
  9. width: 150px;
  10. height: 50px;
  11. background-color: orange;
  12. }
  13. </style>
  14. <script>
  15. $(function () {
  16. $('.clickme').live('click', function() {
  17. alert("增加一个新的");
  18. $('body').append('<div class="clickme">Another target</div>');
  19. });
  20. })
  21. </script>
  22. </head>
  23. <body>
  24. <div class="clickme">
  25. 点我点我点我啊
  26. </div>
  27. </body>
  28. </html>

效果:

Center 5Center 6
9.冒泡事件

代码:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <script src="jquery-1.4.2.min.js"></script>
  7. <script>
  8. $(function () {
  9. $('#div1').click(function () {
  10. alert("点击了外层的div");
  11. })
  12. $('#div2').click(function () {
  13. alert("点击了内层的div");
  14. })
  15. $('a').click(function (evt) {
  16. alert("点击了超链接")
  17. })
  18. })
  19. </script>
  20. </head>
  21. <body>
  22. <div id="div1"style="background-color: orange;width:200px;height: 200px">
  23. <div id="div2" style="background-color: blue;width:150px;height: 150px">
  24. <p>
  25. <a href="#" style="color: white">点击我</a>
  26. </p>
  27. </div>
  28. </div>
  29. </body>
  30. </html>

效果:点击最底层的文字,会依次弹出三个对话框。

若想实现点击哪个出现哪个,则需要在方法里面加入return false

Center 7Center 8
Center 9Center 10

发表评论

表情:
评论列表 (有 0 条评论,282人围观)

还没有评论,来说两句吧...

相关阅读