jQuery learning notes

1, Jquery3 three point one

1. Overview

Introduction: jquery is an excellent lightweight framework for javascript, compatible with css3 and major browsers, and provides simple operations such as dom, events, animate, ajax, etc. Moreover, jquery has rich plug-ins, and most functions have corresponding plug-in solutions.

Jquery is a JS file, but it simplifies JS.

Jquery was founded by American John Resig in 2006.

Official website: http://jquery.com/

Divided into 1 X,2.X,3. The methods provided by the three major versions of X are basically the same, only 2 X 3.X is no longer compatible with lower version browsers such as IE 6, 7 and 8.

purpose: Write less, do more.	Write less, do more.
    

Features: jQuery is a JavaScript function library.

The jQuery library contains the following features:

  • HTML element selection
  • HTML element operation
  • CSS operation
  • HTML event function
  • JavaScript effects and animation
  • HTML DOM traversal and modification
  • AJAX
  • Utilities

introduce:

<script src="jquery-3.3.1.min.js></script>		If the label is used to lead the package, it is not allowed to write anything in it js sentence!
    
    
  1. <script src= "../js/jquery-3.3.1.min.js"></script>
  2. <script>
  3. alert( "hello world.");
  4. </script>

Note: to use jQuery, you must first introduce package and jquery-3.3.1 Min.js annotates and shortens the length of variables, which is relatively small, so this min.js package is referenced

 

2. Entry function

$() or jQuery() is called jq selector environment. Just put quotation marks inside and fill in the relevant selector to obtain the matching elements.

 

    
  1. js: window.οnlοad= function(...){}
  2. jq: $( document).ready( function(){...});
  3. $( function(){...});
    
  1. 1. Both functions are consistent, which can make the behavior of obtaining elements occur after rendering elements;
  2. 2. JS Only one entry is allowed. If multiple entries are written, the latter will cover the former;
  3. 3. JQ Multiple entries are allowed and exist in parallel, which will take effect;
  4. 4. JS The portal needs to wait until all resources on the page are loaded, and JQ The entry only needs to wait for the label on the page to be rendered, JQ The entrance speed is faster.

demonstration:

    
  1. <script src="../js/jquery-3.3.1.min.js"> </script>
  2. <script>
  3. // JS page loading completed
  4. window.onload = function () {
  5. alert( "test1");
  6. }
  7. window.onload = function () {
  8. alert( "test2");
  9. }
  10. // The $symbol is short for jquery
  11. // Complex writing:
  12. $( document).ready( function () {
  13. alert( "test3");
  14. });
  15. // Simplified writing
  16. $( function () {
  17. alert( "test4");
  18. })
  19. </script>

3. Events

    
  1. js: js object.onclick = function(){...}
  2. jq: jquery object.click( function(){...})
  3. be careful: jq The event types in are unified and do not add on

demonstration:

js code:

    
  1. <script src= "../js/jquery-3.3.1.min.js"></script>
  2. <script>
  3. // Note: JS code is written after = jquery code is basically written in ()
  4. // jquery entry function:
  5. $( function () {
  6. // JS code:
  7. var btn = document.getElementById( "btn");
  8. btn.onclick = function () {
  9. alert( "The button was clicked...");
  10. }
  11. });
  12. </script>

jQuery Code:

    
  1. <script src= "../js/jquery-3.3.1.min.js"></script>
  2. <script>
  3. // Note: JS code is written after = jquery code is basically written in ()
  4. // jquery entry function:
  5. jQuery( function () {
  6. // jquery Code:
  7. $( "#btn").click( function () {
  8. alert( "The button was clicked...");
  9. });
  10. });
  11. </script>
  <input type="button" value="Button" id = "btn"/>
    

4. Overall operation

    
  1. 1,stay jq Inside, through $()The returned array allows the developer to manipulate the whole
  2. 2,Select one of the elements in the array: js object[subscript]           jq object.e q(subscript) 

html code:

    
  1. <body>
  2. <input type="button" value="Button 1111" />
  3. <input type="button" value="Button 2222" />
  4. </body>

js code:

    
  1. <script src= "../js/jquery-3.3.1.min.js"></script>
  2. <script>
  3. // jquery entry function:
  4. $( function () {
  5. // JS code binds events for two buttons (JS code cannot bind as a whole)
  6. // Document. In JS GetElementsByTagName this method returns an array. Only the corresponding element can be bound
  7. var inputs = document.getElementsByTagName( "input");
  8. inputs[ 0].onclick = function () {
  9. alert( "Button 1111 was clicked...");
  10. }
  11. inputs[ 1].onclick = function () {
  12. alert( "Button 2222 was clicked...");
  13. }
  14. });
  15. </script>

jQuery Code:

A. JQuery overall operation binding

    
  1. <script src= "../js/jquery-3.3.1.min.js"></script>
  2. <script>
  3. // jquery entry function:
  4. $( function () {
  5. // jquery writing:
  6. $( "input").click( function () {
  7. alert( "The button was clicked...");
  8. });
  9. });
  10. </script>

B. A single tag implements event binding

    
  1. <script src= "../js/jquery-3.3.1.min.js"></script>
  2. <script>
  3. // jquery entry function:
  4. $( function () {
  5. // jquery writing:
  6. $( "input").eq( 0).click( function () {
  7. alert( "Button 1111 was clicked...");
  8. });
  9. $( "input").eq( 1).click( function () {
  10. alert( "Button 2222 was clicked...");
  11. });
  12. });
  13. </script>

5. Object rotation

explain:

Although jquery is essentially js, the properties and methods of jquery must ensure that the objects are obtained through jquery, the objects obtained by js are js objects, and the objects obtained by jquery are jquery objects. If you want to use them interchangeably, you must rotate the objects to each other.

Format:

    
  1. js Object to jq Object: $(js object)
  2. jq Object to js Object: jq object[Indexes] or jq object.get(Indexes)

demonstration:

html code:

    
  1. <body>
  2. <input type="button" value="Button" id="btn" />
  3. </body>

js to jQuery object

    
  1. <script src= "../js/jquery-3.3.1.min.js"></script>
  2. <script>
  3. // jquery entry function:
  4. $( function () {
  5. // JS object to jquery object
  6. var btn = document.getElementById( "btn");
  7. // JS object calls jquery method (not working)
  8. /*btn.click(function () {
  9. alert("The button was clicked ");
  10. });*/
  11. // Requirement: convert JS object into jquery object Just give me some money
  12. $(btn).click( function () {
  13. alert( "The button was clicked...");
  14. });
  15. });
  16. </script>

jQuery to js object

    
  1. <script src= "../js/jquery-3.3.1.min.js"></script>
  2. <script>
  3. // jquery entry function:
  4. $( function () {
  5. // jquery object to JS object
  6. /*$("#btn").onclick = function () {
  7. alert("The button was clicked ");
  8. }*/
  9. : jq object[Indexes] or jq object.get(Indexes)
  10. // Method 1: [subscript]
  11. /*$("#btn")[0].onclick = function () {
  12. alert("The button was clicked ");
  13. }*/
  14. // Method 2: get (subscript)
  15. $( "#btn").get( 0).onclick = function () {
  16. alert( "The button was clicked...");
  17. }
  18. });
  19. </script>

6. Control css

6.1. Single attribute access:

jq object.css('width');
    

6.2. Single attribute modification:

jq object.css('width','100px');
    

6.3. Multi attribute modification:

    
  1. jq object.css({ 'width': '100px', 'height': '100px'});
  2. You can modify more than one at a time css attribute

demonstration:

html code:

    
  1. <body>
  2. <div id="box"> </div>
  3. </body>

js code:

    
  1. <script src= "../js/jquery-3.3.1.min.js"></script>
  2. <script>
  3. $( function () {
  4. // JS control style: Style
  5. var box = document.getElementById( "box");
  6. box.style.width = "100px";
  7. box.style.height = "100px";
  8. box.style.backgroundColor = "pink";
  9. });
  10. </script>

jQuery Code:

    
  1. <script src="../js/jquery-3.3.1.min.js"> </script>
  2. <script>
  3. $( function () {
  4. // jquery control style: css
  5. $( "#box").css({
  6. width: "100px",
  7. height: "100px",
  8. backgroundColor: "skyblue"
  9. });
  10. // Single attribute:
  11. $( "#box").css( "backgroundColor", "red");
  12. });
  13. </script>

7. Control label properties

7.1. Single attribute access:

jq object.attr('class');          
    

7.2. Single attribute modification:

jq object.attr('class ','myClass');  
    

7.3. Multi attribute modification:

    
  1. jq object.attr({ 'class': 'myClass', 'id': 'myId'});
  2. You can modify more than one at a time attr attribute

demonstration:

html code:

    
  1. <body>
  2. <div id="box" class="test" title="bbb"> </div>
  3. </body>

js code: view in browser F12 / developer mode

    
  1. <script src= "../js/jquery-3.3.1.min.js"></script>
  2. <script>
  3. $( function () {
  4. // Requirement: modify the class and title attribute values of the box tag
  5. var box = document.getElementById( "box");
  6. box.className = "bbb";
  7. box.title = "There's nothing here";
  8. });
  9. </script>

jQuery Code:

    
  1. <script src= "../js/jquery-3.3.1.min.js"></script>
  2. <script>
  3. $( function () {
  4. // Requirement: modify the class and title attribute values of the box tag
  5. $( "#box").attr( "class", "aaa");
  6. $( "#box").attr( "title", "There's nothing here");
  7. });
  8. </script>

7.4. Delete attribute:

jq object.removeAttr('class ');  
    

demonstration:

html code:

    
  1. <body>
  2. <input type="checkbox" id="ck">
  3. <input type="button" value="Click" id="btn">
  4. </body>

jQuery Code:

    
  1. <script src= "../js/jquery-3.3.1.min.js"></script>
  2. <script>
  3. $( function () {
  4. // Requirement: click the button to delete the id attribute of the checkbox tag
  5. $( "#btn").click( function () {
  6. $( "#ck").removeAttr( "id");
  7. });
  8. });
  9. </script>

be careful:

We can also use prop() to obtain attributes, which is complementary to attr(). Generally speaking, we use attr() to obtain tag attributes, but sometimes we can't obtain them. At this time, we can use prop() to obtain them, such as the checked attribute of form elements.

jq object.prop();
    

prop() is used for attributes with both true and false attributes, such as checked, selected or disabled. attr() is used for others

demonstration:

    
  1. <script src= "../js/jquery-3.3.1.min.js"></script>
  2. <script>
  3. $( function () {
  4. // Requirement: click the click button to view the checked attribute value of the checkbox option box
  5. $( "#btn").click( function () {
  6. // attr can obtain attributes, but the checked attribute of the option box cannot be obtained
  7. var result = $( "#ck").attr( "checked");
  8. alert( "result = " + result);
  9. // Abbreviation for prop attribute
  10. result = $( "#ck").prop( "checked");
  11. alert( "result = " + result);
  12. });
  13. });
  14. </script>

 

8. Case 1: whether to accept the agreement

The picture is shown below:

8.1 Description:

    
  1. The button itself is disabled Disabled status
  2. When the check box is checked, the button becomes enabled, and then the style changes
  3. Button style changes have been made through a specific class The name is written in style, as long as you check the button class You can modify the name

8.2. Demonstration:

html code:

    
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title> </title>
  6. <style type="text/css">
  7. /*input The label contains the class name of submit*/
  8. input .submit {
  9. background: #69b946;
  10. display: inline-block;
  11. height: 52px;
  12. width: 306px;
  13. text-align: center;
  14. cursor: pointer;
  15. font: 22px/ 52px "Microsoft YaHei ";
  16. color: #fff;
  17. border-radius: 3px;
  18. border-style: solid;
  19. border-width: 1px;
  20. border-color: transparent;
  21. }
  22. input .disabled {
  23. background: #f4f9fd;
  24. color: #888;
  25. cursor: default;
  26. border-color: #d0dae3;
  27. cursor: default;
  28. outline: none;
  29. }
  30. </style>
  31. </head>
  32. <body>
  33. <form action="https://www.baidu.com">
  34. <input id="kuang" type="checkbox" />
  35. <label for="kuang">agree"Terms of service"and"User instructions","Privacy related policies" </label>
  36. <br />
  37. <br />
  38. <input type="submit" disabled="disabled" class="submit disabled" id="btn" />
  39. </form>
  40. </body>
  41. </html>

jQuery Code:

    
  1. <script src="../js/jquery-3.3.1.min.js"> </script>
  2. <script>
  3. $( function () {
  4. // 1. Get the option box and bind the click event
  5. $( "#kuang").click( function () {
  6. // 2. Get the status of the option box
  7. // var checked = $("#kuang").attr("checked"); it won 't work
  8. var checked = $( "#kuang").prop( "checked");
  9. // alert("checked = " + checked);
  10. // 3. Judgment
  11. if (checked == true) {
  12. // Modify the attribute value of btn submit button`
  13. $( "#btn").attr({
  14. "disabled" : false,
  15. class: "submit"
  16. });
  17. } else {
  18. $( "#btn").attr({
  19. "disabled" : true,
  20. class: "submit disabled"
  21. });
  22. }
  23. });
  24. })
  25. </script>

 

9. val() function

For the value of the value attribute of the form element, a method is separately encapsulated in jq, which cannot be obtained correctly by using attr().

Format:

jq object.val();
    

Note: when the val() function has parameters, modify the value value; When there is no parameter, the value value is returned.

demonstration:

html code:

    
  1. <body>
  2. <input type="text" id="text" value="Please enter the content..." />
  3. <input type="button" value="Click" id="btn" />
  4. </body>

js code:

    
  1. <script src= "../js/jquery-3.3.1.min.js"></script>
  2. <script>
  3. $( function () {
  4. // Requirement: click the btn button to obtain the value in the text box
  5. // JS mode:
  6. var btn = document.getElementById( "btn");
  7. var text = document.getElementById( "text");
  8. btn.onclick = function () {
  9. var value = text.value;
  10. alert( "value = " + value);
  11. }
  12. });
  13. </script>

jQuery Code:

    
  1. <script src= "../js/jquery-3.3.1.min.js"></script>
  2. <script>
  3. $( function () {
  4. // Requirement: click the btn button to obtain the value in the text box
  5. // jquery get properties
  6. $( "#btn").click( function () {
  7. // Method 1: attr doesn't work
  8. var value = $( "#text").attr( "value");
  9. alert(value);
  10. // Mode 2: prop insurance, safety
  11. value = $( "#text").prop( "value");
  12. alert(value);
  13. // Method 3: val();
  14. value = $( "#text").val();
  15. alert(value);
  16. });
  17. });
  18. </script>

 

10. Case 2: online search box

10.1 Description:

    
  1. 1. When the text input box gets the focus, the default text inside is cleared. When the focus is lost, the text inside will come back.
  2. 2. When there is content entered by the user, the text cannot be cleared when obtaining the focus. You can't restore text even if you lose focus.

10.2. Demonstration:

html code:

    
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title> </title>
  6. <style type="text/css">
  7. * {
  8. margin: 0;
  9. padding: 0;
  10. border: 0;
  11. list-style: none;
  12. }
  13. .box {
  14. height: 40px;
  15. width: 258px;
  16. margin: 100px auto 0;
  17. }
  18. .box input {
  19. float: left;
  20. width: 210px;
  21. padding-left: 6px;
  22. height: 40px;
  23. background: url(../img/left.jpg);
  24. color: #ccc;
  25. }
  26. .box button {
  27. float: right;
  28. width: 42px;
  29. height: 40px;
  30. background: url(../img/right.jpg);
  31. }
  32. </style>
  33. </head>
  34. <body>
  35. <div class="box">
  36. <input type="text" value="Please enter text..." />
  37. <button> </button>
  38. </div>
  39. </body>
  40. </html>

jQuery implementation method 1:

    
  1. <script src="../js/jquery-3.3.1.min.js"> </script>
  2. <script>
  3. $( function () {
  4. // Requirement: bind the focus event for the input tag
  5. $( "input").focus( function () {
  6. // 1. Get the value of the input tag
  7. var value = $( "input").val();
  8. // alert(value);
  9. // 2. Judgment
  10. if (value == "Please enter text...") {
  11. // 3. Clear the text and set the style at the same time
  12. $( "input").val( "").css( "color", "black");
  13. }
  14. });
  15. // Requirement: bind blur focus event for input tag
  16. $( "input").blur( function () {
  17. if ($( "input").val() == "") {
  18. $( "input").val( "Please enter text...").css( "color", "gray");
  19. }
  20. });
  21. })
  22. </script>

jquery implementation mode 2:

    
  1. <script src="../js/jquery-3.3.1.min.js"> </script>
  2. <script>
  3. $( function () {
  4. // Requirement: bind focus and defocus events for the input tag
  5. $( "input").focus( function () {
  6. // 1. Get the value of the input tag
  7. var value = $( "input").val();
  8. // alert(value);
  9. // 2. Judgment
  10. if (value == "Please enter text...") {
  11. // 3. Clear the text and set the style at the same time
  12. $( "input").val( "").css( "color", "black");
  13. }
  14. }).blur( function () {
  15. if ($( "input").val() == "") {
  16. $( "input").val( "Please enter text...").css( "color", "gray");
  17. }
  18. });
  19. })
  20. </script>

11. Control label content

    
  1. jq object .html();
  2. Write parameters in parentheses to represent modification, and do not write to represent acquisition.

demonstration:

    
  1. <body>
  2. <div id="box"> </div>
  3. </body>

js code:

    
  1. <script src="../js/jquery-3.3.1.min.js"> </script>
  2. <script>
  3. $( function () {
  4. // 1. Get box container
  5. document.getElementById( "box").innerHTML = "<h1>Hello, JS.</h1>";
  6. })
  7. </script>

JQuery Code:

    
  1. <script src="../js/jquery-3.3.1.min.js"> </script>
  2. <script>
  3. $( function () {
  4. // 1. Get box container
  5. $( "#box").html( "<h1>Hello, world!</h1>");
  6. })
  7. </script>

 

12. Control class

    
  1. Jq object.addClass() Add class
  2. Jq object.removeClass() Delete class
  3. Jq object.hasClass() Determine whether there are classes
  4. Jq object.toggleClass() Switch class

demonstration:

html code:

    
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title> </title>
  6. <style>
  7. .box{
  8. width: 200px;
  9. height: 200px;
  10. background: pink;
  11. }
  12. .bgcolor{
  13. background: skyblue;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <div class="box"> </div>
  19. <input type="button" value="newly added class" />
  20. <input type="button" value="remove class" />
  21. <input type="button" value="switch class" />
  22. <input type="button" value="Judge whether there is class" />
  23. </body>
  24. </html>

jQuery Code:

    
  1. <script src= "../js/jquery-3.3.1.min.js"></script>
  2. <script>
  3. $( function () {
  4. var inputs = $( "input");
  5. // Add class
  6. inputs.eq( 0).click( function () {
  7. $( ".box").addClass( "bgcolor");
  8. });
  9. // Remove class
  10. inputs.eq( 1).click( function () {
  11. $( ".box").removeClass( "bgcolor");
  12. });
  13. // Switch class
  14. inputs.eq( 2).click( function () {
  15. $( ".box").toggleClass( "bgcolor");
  16. });
  17. // Determine whether class exists
  18. inputs.eq( 3).click( function () {
  19. var result = $( ".box").hasClass( "bgcolor");
  20. alert( "result = " + result);
  21. });
  22. });
  23. </script>

Original link: https://blog.csdn.net/sswqzx/article/details/82834407

Keywords: JQuery

Added by immunity on Tue, 11 Jan 2022 00:09:42 +0200