a library. make js a lot easier. jQuery takes a lot of common tasks that require many lines of JavaScript code to accomplish, and wraps them into methods that you can call with a single line of code.
1
eg. document.querySelector("h1") = $("h1")
Incorporate jQuery
1 2 3 4 5 6 7
<body> . . . <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.0/jquery.min.js"></script> //this one is from the website, copy the link <script src="index.js" charset="UTF-8"></script> </body>
Place the script tag above the index.js
Ready Function
1 2 3
$(document).ready(function () {
});
if you put the jQuery tag in the head tag instead of the end of the body tag, then you need to use this ready function in js file. It means that, once our ducument is ready and then we gonna call the call back function.
Minify
Minification refers to the process of removing unnecessary or redundant data without affecting how the resource is processed by the browser - e.g. code comments and formatting, removing unused code, using shorter variable and function names, and so on.
Manipulating Styles
1 2 3 4 5 6 7 8 9 10 11 12 13
$("h1").css("color", "green"); // setting the value
console.log($("h1").css("color")); // getting the value
$("h1").on("mouseover", function () { $("h1").css("color", "green"); }); $("h1").on("click", function () { $("h1").css("color", "green"); });
adding and removing
1 2 3 4 5 6 7 8 9 10 11
//before, after -> add the new element beofre the opening tag of the selected element $("h1").before("<button>new</button>"); // create the element before the element we selected $("h1").after("<button>new</button>");
//prepend -> add the new element just before the content of the h1 and right after the opening tag $("h1").prepend("<button>new</button>"); // added right after the content of h1 $("h1").append("<button>new</button>");
//customize $("button").on("click",function(){ $("h1").animate({opacity: 0.5}); // this method can only change a numeric value $("h1").slideUp().slideDown().animate({opacity: 0.5}); })