The most important thing to use jquery is to be familiar with its properties, methods, etc. The summary below is really good, and the frequently used ones are summarized
1. References about page elements
Referencing elements through jquery's $() includes methods such as id, class, element name, hierarchical relationship of elements, dom or xpath conditions, etc., and the returned object is a jquery object ( Collection object), you cannot directly call methods defined by DOM.
2. Conversion between jQuery objects and dom objects
Only jquery objects can use the methods defined by jquery. Note that there is a difference between dom objects and jquery objects. When calling methods, you should pay attention to whether you are operating on dom objects or jquery objects.
Ordinary dom objects can generally be converted into jquery objects through $().
For example: $(document.getElementById("msg")) is a jquery object, and you can use jquery methods.
Because the jquery object itself is a collection. Therefore, if the jquery object is to be converted into a dom object, one of the items must be retrieved, which can generally be retrieved through an index.
For example: $("#msg")[0], $("p").eq(1)[0], $("p").get()[1], $("td" )[5] These are dom objects, you can use methods in dom, but you can no longer use Jquery methods.
The following writing methods are correct:
$(”#msg”).html();
$(”#msg”)[0].innerHTML;
$(”# msg”).eq(0)[0].innerHTML;
$(”#msg”).get(0).innerHTML;
3. How to get an item of jQuery collection
For the obtained element collection, to obtain an item (specified by index), you can use the eq or get(n) method or the index number. Note that eq returns a jquery object, while get(n) and index return is a dom element object. For jquery objects, you can only use jquery methods, and for dom objects, you can only use dom methods. For example, you want to get the content of the third
element. There are the following two methods:
$("p").eq(2).html(); //Call the jquery object method
$("p").get(2).innerHTML; / /Call the method attribute of dom
4. The same function implements set and get
This is true for many methods in Jquery, mainly including the following:
$("#msg").html (); //Return the html content of the element node with id msg.
$("#msg").html("new content");
//Write "new content" as an html string into the content of the element node with the id of msg, and the page will display bold new content
$("#msg").text(); //Return the text content of the element node with id msg.
$("#msg").text("new content");
//Write "new content" as a normal text string into the content of the element node with id msg, and the page will display new content
$(”#msg”).height(); //Return the height of the element with id msg
$(”#msg”).height(”300″); //Return the id to msg Set the height of the element to 300
$("#msg").width(); //Return the width of the element with id msg
$("#msg").width("300"); //Set the width of the element with id msg to 300
$("input").val("); //Return the value of the form input box
$("input"). val("test"); //Set the value of the form input box to test
$("#msg").click(); //Trigger the click event of the element with id msg
$(”#msg”).click(fn); //Add a function for the element click event with id msg
Similarly, blur, focus, select, and submit events can have two calling methods
5. Collection processing function
We do not need to loop through the collection content returned by jquery and process each object separately. jquery has provided us with very convenient methods for processing collections
including. Two forms:
$("p").each(function(i){this.style.color=['#f00′,'#0f0′,'#00f'][ i ]})
//Set different font colors for the p elements with indexes 0, 1, and 2 respectively.
$("tr").each(function(i){this.style.backgroundColor=[. '#ccc','#fff'][i%2]})
//Achieving the effect of interlaced color changing of tables
$(”p”).click(function(){alert( $(this).html())})
//Added a click event for each p element. Clicking on a p element will pop up its content
6. Expand the functions we need
$.extend({
min: function(a, b){return a max: function(a, b){return a > b?a :b; }
}); //Extended min and max methods for jquery
Use the extended method (called through "$.method name"):
alert("a=10, b=20,max=” $.max(10,20) ”,min=” $.min(10,20));
7. Support the continuous writing of methods
The so-called continuous writing means that you can Continuously call various methods on a jquery object.
For example:
$("p").click(function(){alert($(this).html())})
.mouseover(function(){alert('mouse over event ')})
.each(function(i){this.style.color=['#f00′,'#0f0′,'#00f'][ i ]});
8 , operating the style of the element
mainly includes the following methods:
$("#msg").css("background"); //Return the background color of the element
$("#msg") .css("background","#ccc") //Set the background of the element to gray
$("#msg").height(300); $("#msg").width("200") ; //Set width and height
$(”#msg”).css({ color: “red”, background: “blue” });//Set styles in the form of name-value pairs
$(”#msg”).addClass(” select”); //Add a class named select to the element
$(”#msg”).removeClass(”select”); //Remove the class named select for the element
$(”#msg” ).toggleClass("select"); //If it exists (does not exist), delete (add) the class named select
9. Complete event processing function
Jquery has provided us with various With this event processing method, we don't need to write events directly on the html element, but can directly add events to the objects obtained through jquery.
For example:
$("#msg").click(function(){alert("good")}) //Add a click event to the element
$("p").click (function(i){this.style.color=['#f00′,'#0f0′,'#00f'][ i ]})
//Set three different p element click events respectively Different handling
Several custom events in jQuery:
(1) hover(fn1, fn2): A method that imitates hover events (mouse moves over an object and out of the object). When the mouse moves over a matching element, the first specified function will be triggered. When the mouse moves out of this element, the specified second function will be triggered.
//When the mouse is placed on a row of the table, set the class to over and set it to out when leaving.
$("tr").hover(function(){
$(this).addClass("over");
},
function(){
$(this) .addClass(”out”);
});
(2) ready(fn): Bind a function to be executed when the DOM is loaded and ready for query and manipulation.
$(document).ready(function(){alert("Load Success")})
//When the page is loaded, it prompts "Load Success", which is equivalent to the onload event. Equivalent to $(fn)
(3) toggle(evenFn,oddFn): Switch the function to be called every time it is clicked. If a matching element is clicked, the first function specified is triggered, and when the same element is clicked again, the second function specified is triggered. Each subsequent click repeats the call to these two functions in turn.
//Rotate adding and deleting the class named selected every time you click.
$("p").toggle(function(){
$(this).addClass("selected");
},function(){
$(this).removeClass( "selected");
});
(4) trigger(eventtype): Trigger a certain type of event on each matching element.
For example:
$("p").trigger("click"); //Trigger the click event of all p elements
(5) bind(eventtype,fn), unbind(eventtype): event Binding and unbinding
removes (adds) the bound event from each matching element.
For example:
$("p").bind("click", function(){alert($(this).text());}); //Add click for each p element Event
$("p").unbind(); //Delete all events on all p elements
$("p").unbind("click") //Delete all single events on all p elements Click event
10. Several practical special effects functions
Among them, the toggle() and slidetoggle() methods provide state switching functions.
For example, the toggle() method includes hide() and show() methods.
The slideToggle() method includes the slideDown() and slideUp methods.
11. Several useful jQuery methods
$.browser. Browser type: Detect browser type. Valid parameters: safari, opera, msie, mozilla. For example, if you check whether it is IE: $.browser.isie, if it is an IE browser, it will return true.
$.each(obj, fn): General iteration function. Can be used to iterate over objects and arrays approximately (instead of looping).
For example,
$.each( [0,1,2], function(i, n){ alert( “Item #” i “: ” n); });
is equivalent to:
var tempArr=[0,1,2];
for(var i=0;i
alert(”Item #” i ”: “ tempArr[ i ]);
}
You can also process json data, such as
$.each( { name: “John”, lang: “JS” }, function(i, n){ alert( “Name: ” i “, Value: ” n ); });
The result is:
Name:name, Value:John
Name:lang, Value:JS
$.extend(target,prop1,propN): Use one or more other objects To extend an object and return the extended object. This is the inheritance method implemented by jquery. For example:
$.extend(settings, options);
//Merge settings and options and merge them. The result is returned to settings, which is equivalent to options inheriting setting and saving the inheritance result in setting.
var settings = $.extend({}, defaults, options);
//Merge defaults and options, and merge them. The result is returned to the setting without overwriting the default content.
can have multiple parameters (combine multiple items and return)
$.map(array, fn): Array mapping (processing conversion). After), save it to another new array, and return the generated new array. For example:
var tempArr=$.map( [0,1,2], function(i){ return i 4; });
tempArr content is: [4,5,6]
var tempArr=$.map( [0,1,2], function(i){ return i > 0 ? i 1 : null; });
tempArr content is: [2,3]
$ .merge(arr1,arr2): Merges two arrays and removes duplicate items.
For example: $.merge( [0,1,2], [2,3,4] ) //Return [0,1,2,3,4]
$.trim(str): Delete Whitespace characters at both ends of the string.
For example: $.trim(" hello, how are you? "); //Return "hello, how are you? "
12. Solve conflicts between custom methods or other class libraries and jQuery
Many times we define the $(id) method ourselves to get an element, or some other js libraries such as prototype also define the $ method. If these contents are put together at the same time, it will cause variable method definition conflicts. , Jquery specifically provides a method to solve this problem.
Use the jQuery.noConflict(); method in jquery to transfer control of the variable $ to the first library that implements it or the previously customized $ method. When using Jquery later, just replace all $ with jQuery. For example, the original reference object method $("#msg") is changed to jQuery("#msg").
For example:
jQuery.noConflict();
// Start using jQuery
jQuery(”p p”).hide();
// Use other libraries $()
$("content").style.display = 'none';

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)