Seletores jQuery


Índice

    Mostrar índice


jQuery versus JavaScript

jQuery foi criado em 2006 por John Resig. Ele foi projetado para lidar com incompatibilidades de navegador e simplificar a manipulação de HTML DOM, manipulação de eventos, animações e Ajax.

Por mais de 10 anos, jQuery tem sido a biblioteca JavaScript mais popular do mundo.

No entanto, após o JavaScript versão 5 (2009), a maioria dos utilitários jQuery podem ser resolvidos com algumas linhas de JavaScript padrão:


Encontrando elemento HTML por ID

Retorne o elemento com id="id01":

jQuery

myElement = $("#id01");

Experimente você mesmo →

<!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>

JavaScript

myElement = document.getElementById("id01");

Experimente você mesmo →

<!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>

Encontrando elementos HTML por nome de tag

Retorne todos os elementos <p>:

jQuery

myElements = $("p");

Experimente você mesmo →

<!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>

JavaScript

myElements = document.getElementsByTagName("p");

Experimente você mesmo →

<!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>


Encontrando elementos HTML por nome de classe

Retorne todos os elementos com class="intro".

jQuery

myElements = $(".intro");

Experimente você mesmo →

<!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>

JavaScript

myElements = document.getElementsByClassName("intro");

Experimente você mesmo →

<!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>

Encontrando elementos HTML por seletores CSS

Retorne uma lista de todos os elementos <p> com class="intro".

jQuery

myElements = $("p.intro");

Experimente você mesmo →

<!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>

JavaScript

myElements = document.querySelectorAll("p.intro");

Experimente você mesmo →

<!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>