jQuery byl vytvořen v roce 2006 Johnem Resigem. Byl navržen tak, aby zvládl nekompatibilitu prohlížečů a zjednodušil manipulaci s HTML DOM, manipulaci s událostmi, animace a Ajax.
Již více než 10 let je jQuery nejpopulárnější JavaScriptovou knihovnou na světě.
Po verzi JavaScriptu 5 (2009) však lze většinu nástrojů jQuery vyřešit pomocí několika řádků standardního JavaScriptu:
Vraťte prvek s id="id01":
myElement = $("#id01");
Zkuste to sami →
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
</head>
<body>
<h2>Finding HTML Elements by Id</h2>
<p id="id01">Hello World!</p>
<p id="id02">Hello Sweden!</p>
<p id="id03">Hello Japan!</p>
<p id="demo"></p>
<script>
$(document).ready(function() {
var myElements = $("#id01");
$("#demo").text("The text from the id01 paragraph is: " + myElements[0].innerHTML);
});
</script>
</body>
</html>
myElement = document.getElementById("id01");
Zkuste to sami →
<!DOCTYPE html>
<html>
<body>
<h2>Finding HTML Elements by Id</h2>
<p id="id01">Hello World!</p>
<p id="id02">Hello Sweden!</p>
<p id="id03">Hello Japan!</p>
<p id="demo"></p>
<script>
const myElement = document.getElementById("id01");
document.getElementById("demo").innerHTML = "The text from the id01 paragraph is: " + myElement.innerHTML;
</script>
</body>
</html>
Vrátit všechny prvky <p>:
myElements = $("p");
Zkuste to sami →
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
</head>
<body>
<h2>Finding HTML Elements by Tag Name</h2>
<p>Hello World!</p>
<p>Hello Sweden!</p>
<p>Hello Japan!</p>
<p id="demo"></p>
<script>
$(document).ready(function() {
var myElements = $("p");
$("#demo").text("The text in the first paragraph is: " + myElements[0].innerHTML);
});
</script>
</body>
</html>
myElements = document.getElementsByTagName("p");
Zkuste to sami →
<!DOCTYPE html>
<html>
<body>
<h2>Finding HTML Elements by Tag Name</h2>
<p>Hello World!</p>
<p>Hello Sweden!</p>
<p>Hello Japan!</p>
<p id="demo"></p>
<script>
const myElements = document.getElementsByTagName("p");
document.getElementById("demo").innerHTML = "The text in the first paragraph is: " + myElements[0].innerHTML;
</script>
</body>
</html>
Vraťte všechny prvky s class="intro".
myElements = $(".intro");
Zkuste to sami →
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
</head>
<body>
<h2>Finding HTML Elements by Class Name</h2>
<p class="intro">Hello World!</p>
<p class="intro">Hello Sweden!</p>
<p class="intro">Hello Japan!</p>
<p id="demo"></p>
<script>
$(document).ready(function() {
var myElements = $(".intro");
$("#demo").text("The first paragraph with class='intro' is: " + myElements[0].innerHTML);
});
</script>
</body>
</html>
myElements = document.getElementsByClassName("intro");
Zkuste to sami →
<!DOCTYPE html>
<html>
<body>
<h2>Finding HTML Elements by Class Name</h2>
<p class="intro">Hello World!</p>
<p class="intro">Hello Sweden!</p>
<p class="intro">Hello Japan!</p>
<p id="demo"></p>
<script>
const myElements = document.getElementsByClassName("intro");
document.getElementById("demo").innerHTML = "The first paragraph with class='intro' is: " + myElements[0].innerHTML;
</script>
</body>
</html>
Vrátí seznam všech prvků <p> s class="intro".
myElements = $("p.intro");
Zkuste to sami →
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
</head>
<body>
<h2>Finding HTML Elements by Query Selector</h2>
<p class="intro">Hello World!</p>
<p class="intro">Hello Sweden!</p>
<p class="intro">Hello Japan!</p>
<p id="demo"></p>
<script>
$(document).ready(function() {
var myElements = $("p.intro");
$("#demo").text("The first paragraph with class='intro' is: " + myElements[0].innerHTML);
});
</script>
</body>
</html>
myElements = document.querySelectorAll("p.intro");
Zkuste to sami →
<!DOCTYPE html>
<html>
<body>
<h2>Finding HTML Elements by Query Selector</h2>
<p class="intro">Hello World!</p>
<p class="intro">Hello Sweden!</p>
<p class="intro">Hello Japan!</p>
<p id="demo"></p>
<script>
const myElements = document.querySelectorAll("p.intro");
document.getElementById("demo").innerHTML =
"The first paragraph with class='intro' is: " + myElements[0].innerHTML;
</script>
</body>
</html>