1. Web Design
  2. HTML/CSS
  3. JavaScript for Designers

Grundlegender Spickzettel: Konvertieren Sie jQuery in JavaScript

Scroll to top

German (Deutsch) translation by Władysław Łucyszyn (you can also view the original English article)

Obwohl jQuery immer noch eine beliebte JavaScript-Bibliothek ist und einen Platz in der Webentwicklung hat, haben sich native Web-APIs in den letzten Jahren stark weiterentwickelt. Da moderne Browser eine Vielzahl von JavaScript-Funktionen unterstützen, benötigen wir keine Bibliothek von Drittanbietern mehr, um das DOM relativ einfach zu bearbeiten. Infolgedessen haben sich viele Frontend-Entwickler entschlossen, von jQuery auf JavaScript umzusteigen, um die Anzahl der Abhängigkeiten und damit die Ladezeiten der Seiten zu verringern.

Um Ihnen bei der Umstellung zu helfen, finden Sie hier einen Spickzettel für jQuery zu JavaScript, der die JavaScript-Entsprechungen zu den am häufigsten verwendeten jQuery-Funktionen enthält.

jQuery vs. JavaScript

jQuery ist eine JavaScript-Bibliothek, mit der Webentwickler das Dokumentobjektmodell (DOM) manipulieren können, um dynamisch auf Benutzeraktionen und Statusänderungen zu reagieren. Der größte Vorteil besteht darin, dass Sie auf einfache Weise jedes DOM-Element auf der Seite auswählen und Benutzeraktionen hinzufügen können, indem Sie beispielsweise die "Punktnotation" verwenden:

1
/* Adds the .active class to the toggle button */
2
$('#toggle').addClass('active');

Natives JavaScript ist ausführlicher. Sie können keinem DOM-Element einfach Aktionen hinzufügen. Zuerst müssen Sie es an eine Variable übergeben, dann können Sie verschiedene Aktionen ausführen. So sieht beispielsweise der obige Code in JavaScript aus:

1
let toggle = document.querySelector('#toggle');
2
toggle.classList.add('active');

Obwohl JavaScript ausführlicher ist, müssen Sie keine Bibliothek eines Drittanbieters verwenden, da die DOM-API (die Sie mit Ihrem JavaScript-Code aufrufen) in Ihren Webbrowser integriert ist. Infolgedessen haben Sie eine Abhängigkeit weniger und in den meisten Fällen wird Ihre Seite auch schneller geladen als mit jQuery. Sie müssen jedoch selbst beurteilen, ob es sich lohnt, auf natives JavaScript umzusteigen, da Sie mehr Code schreiben müssen, um das gleiche Ergebnis zu erzielen. Der Kompromiss zwischen jQuery und JavaScript besteht also aus mehr benutzerdefiniertem Code im Vergleich zu weniger Abhängigkeiten.

Sehen wir uns nun die 20 am häufigsten verwendeten jQuery-zu-JavaScript-Entsprechungen an.

1. Wählen Sie Elemente

jQuery:

Die Funktion jQuery(), kurz $(), ist die globale Funktion in jQuery, mit der Sie ein beliebiges Element im DOM auswählen können.

1
/* Syntax */
2
3
jQuery();
4
$();        // shortcut
1
/** 

2
 * Example

3
 * 

4
 * Selects all the links among the descendants of the 'my-class' class

5
 */
6
 
7
jQuery('.my-class a');
8
$('.my-class a');

Weitere Informationen finden Sie in den Dokumenten zur jQuery-API: jQuery() - globale Funktion

JavaScript:

1
/* Syntax */
2
3
Document.querySelectorAll();
1
/* Example */
2
3
document.querySelectorAll('.my-class a');

Weitere Informationen finden Sie in den DOM-API-Dokumenten: .querySelectorAll()-Methode

2. Wählen Sie Erstes Element

jQuery:

1
/* Syntax */
2
3
.first();
1
/**

2
 * Example

3
 * 

4
 * Selects the first link among the descendants of the 'my-class' class

5
 */
6
7
$('.my-class a').first();

Weitere Informationen finden Sie in der jQuery-API docs: first()-Methode

JavaScript:

1
/* Syntax */
2
3
Document.querySelector();
1
/* Example */
2
3
document.querySelector('.my-class a');

Weitere Informationen finden Sie in der DOM-API-Dokumentation: .querySelector()-Methode

3. Elemente finden

jQuery:

1
/* Syntax */
2
3
.find();
1
/**

2
 * Example

3
 * 

4
 * Find all the span tags that are descendants of links within 

5
 * the 'my-class' class

6
 * 

7
 * Note: searches for all descendants not just for children

8
 */
9
 
10
$('.my-class a').find('span');
11
12
$('.my-class a').find('span').css('color', 'red');  // for testing

Weitere Informationen finden Sie in den Dokumenten der jQuery-API: .find()-Methode

JavaScript:

1
/* Syntax */
2
3
// to find the first element (also if there's only one)

4
Document.querySelector();
5
6
// to find all elements

7
Document.querySelectorAll();
1
/**

2
 * Example

3
 * 

4
 * At first querySelectorAll() returns a NodeList, so we have to loop

5
 * through it to find all the span tags we want

6
 * 

7
 * For the sake of testing, I made the selected elements red, 

8
 * you can find the 'style.color' property below in this cheat sheet 

9
 */
10
11
// finds all '.my-class a'

12
let nodes = document.querySelectorAll('.my-class a');
13
14
// loops through all '.my-class a'

15
for (let i = 0; i < nodes.length; i++) {
16
    
17
    // checks if the individual '.my-class a' element has a 

18
    // 'span' descendant to avoid 'undefined' error

19
    if (nodes[i].querySelector('span')) {
20
        
21
      // colors span tags that are desdendants of '.my-class a'

22
      nodes[i].querySelector('span').style.color = 'red';
23
   
24
   }
25
26
}

Sehen Sie Code-Demo:

Please accept marketing cookies to load this content.

Weitere Informationen finden Sie in den DOM-API-Dokumenten: .querySelector()-Methode, .querySelectorAll()-Methode

4. Wählen Sie Children

jQuery:

1
/* Syntax */
2
3
.children();
4
.children('selector');
1
/**

2
 * Example

3
 * 

4
 * (1) Finds all the children of all '.my-class a' elements on the age

5
 * 

6
 * (2) Finds all the 'span' elements that are the children of 

7
 * any '.my-class a' element on the page

8
 * 

9
 * Note: searches only for children (first-level of descendants)

10
 */
11
 
12
$('.my-class a').children();
13
14
$('.my-class a').children('span');
15
16
$('.my-class a').children('span').css('color', 'blue'); // for testing

Weitere Informationen finden Sie in der jQuery API docs: .children()-Methode

JavaScript:

1
/* Syntax */
2
3
ParentNode.children;
1
/**

2
 * Example 

3
 * 

4
 * 2nd example of the jQuery version above, plus makes the selected span 

5
 * tags blue for the sake of easy testing

6
 * 

7
 * for 1st example, only leave out the if check at the end (we need this 

8
 * because the JS version is not a method but a property, so we 

9
 * need to check which children are 'span')

10
 * 

11
 */
12
13
// selects all the elements with 'my-class a' on the page

14
let items = document.querySelectorAll('.my-class a');
15
16
// loops through all the elements with '.my-class a'

17
for (let i = 0; i < items.length; i++) {
18
    
19
        // finds the children of the current '.my-class a' element

20
        let kids = items[i].children;
21
        
22
                // loops through the children of the current '.my-class a' element

23
        for (let j = 0; j < kids.length; j++) {
24
            
25
            // checks if the child element is a span tag

26
            if (kids[j].tagName == 'SPAN') {
27
        
28
                              kids[j].style.color = 'blue';
29
      
30
            }
31
                
32
                }
33
34
}

Sehen Sie Testcode:

Please accept marketing cookies to load this content.

Weitere Informationen finden Sie in den DOM-API-Dokumenten: .children-Eigenschaft - Denken Sie daran, dass die JavaScript-Version zwar eine Eigenschaft ist, die jQuery-Version jedoch eine Methode in Klammern.

5. Wählen Sie Parent

jQuery:

1
/* Syntax */
2
3
.parent();
1
/**

2
 * Example

3
 * 

4
 * Selects the parent elements of ALL elements with 'my-class a' 

5
 * on the page

6
 */
7
 
8
$('.my-class a');

Weitere Informationen finden Sie in der jQuery-API-Dokumentation: .parent()-Methode

JavaScript:

1
/* Syntax */
2
3
Node.parentNode;
1
/** 

2
 * Example 

3
 * 

4
 * Selects the parent of the FIRST element with 'my-class a' on the page 

5
 * (for the sake of less repetition)

6
 * 

7
 * For looping through all '.my-class a' elements, use the looping 

8
 * solution and querySelectorAll() from the two examples above.

9
 */
10
11
let item = document.querySelector('.my-class a');
12
item.parentNode;

Weitere Informationen finden Sie in den DOM-API-Dokumenten: .parentNode-Eigenschaft

6. Wählen Sie Siblings

jQuery:

1
/* Syntax */
2
3
.siblings();
1
/**

2
 * Example

3
 * 

4
 * Selects the siblings of ALL elements with the 'find-siblings' 

5
 * class on the page

6
 */
7
 
8
$('.find-siblings').siblings();

Weitere Informationen finden Sie in der jQuery-API-Dokumentation: .siblings()-Methode

JavaScript:

1
/* Syntax */
2
3
Node.parentNode.querySelectorAll(":not(#elem)");
1
/**

2
 * Example

3
 * 

4
 * Selects the siblings of the FIRST element with the

5
 * 'find-siblings' class

6
 * 

7
 * For looping through all 'find-siblings' elements, see examples #3 and #4

8
 * 

9
 * the ':scope' pseudoclass is necessary for preventing the child 

10
 * elements of 'item' from being selected (otherwise querySelectorAll() 

11
 * searches through all descendants of 'item', with ':scope >' it loops 

12
 * through just the first level)

13
 * 

14
 */
15
16
let item = document.querySelector('.find-siblings');
17
let siblings = item.parentNode.querySelectorAll(':scope > :not(.find-siblings)');

Sehen Sie Testdemo:

Please accept marketing cookies to load this content.

Weitere Informationen finden Sie in den DOM-API-Dokumenten: .parentNode-Eigenschaft, .querySelectorAll() -Methode, :scope-Pseudoklasse (siehe Abschnitt "Direkte untergeordnete Elemente").

7. Wählen Sie Next Sibling

jQuery:

1
/* Syntax */
2
3
.next();
1
/**

2
 * Example

3
 * 

4
 * Selects the next siblings of all elements with 'my-class a' 

5
 * on the page

6
 */
7
 
8
$('.my-class a').next();

Weitere Informationen finden Sie in der jQuery-API-Dokumentation: .next()-Methode

JavaScript:

1
/* Syntax */
2
3
NonDocumentTypeChildNode.nextElementSibling;
1
/**

2
 * Example 

3
 * 

4
 * For declaring the 'item' variable by selecting elements with 

5
 * 'my-class a' on the page, see examples #3, #4, #5

6
 */
7
item.nextElementSibling;

Weitere Informationen finden Sie in den DOM-API-Dokumenten: .nextElementSibling-Eigenschaft

8. Wählen Sie Vorheriges Sibling 

jQuery:

1
/* Syntax */
2
3
.prev();
1
/**

2
 * Example

3
 * 

4
 * Selects the previous siblings of all elements with 'my-class a' 

5
 * on the page

6
 */
7
 
8
$('.my-class a').prev();

Weitere Informationen finden Sie in den Dokumenten der jQuery-API: .prev()-Methode

JavaScript:

1
/* Syntax */
2
3
NonDocumentTypeChildNode.previousElementSibling;
1
/**

2
 * Example 

3
 * 

4
 * For declaring the 'item' variable by selecting elements with 

5
 * 'my-class a' on the page, see examples examples #3, #4, #5

6
 */
7
item.previousElementSibling;

Weitere Informationen finden Sie in den DOM-API-Dokumenten: .previousElementSibling-Eigenschaft

9. Add Class

jQuery:

1
/* Syntax */
2
3
.addClass();
1
/**

2
 * Example 

3
 * 

4
 * Adds the 'second-class' class to every 'my-class' element

5
 * 

6
 */
7
$('.my-class').addClass('second-class');

Weitere Informationen finden Sie in der jQuery-API-Dokumentation: .addClass()-Methode

JavaScript:

1
/* Syntax */
2
3
Element.classList.add();
1
/**

2
 * Example 

3
 * 

4
 * For declaring the 'item' variable by selecting elements with 'my-class' 

5
 * on the page, see examples examples #3, #4, #5

6
 */
7
8
item.classList.add('second-class');

Weitere Informationen finden Sie in den DOM-API-Dokumenten: .classList-Eigenschaft und .add()-Methode

10. Remove Class

jQuery:

1
/* Syntax */
2
3
.removeClass();
1
/**

2
 * Example 

3
 * 

4
 * (1) Removes the 'second-class' class from every 'my-class' element

5
 * 

6
 * (2) Removes 'second-class', then add 'third-class' to 

7
 * every 'my-class' element

8
 */
9
$('.my-class').removeClass('second-class');
10
$('.my-class').removeClass('second-class').addClass('third-class');

Weitere Informationen finden Sie in den Dokumenten der jQuery-API: .removeClass()-Methode

JavaScript:

1
/* Syntax */
2
3
Element.classList.remove();
1
/**

2
 * Example 

3
 * 

4
 * For declaring the 'item' variable by selecting elements with 'my-class' 

5
 * on the page, see examples examples #3, #4, #5

6
 */
7
 
8
item.classList.remove('second-class');
9
10
// To use it together with add(), you need two separate statements

11
item.classList.remove('second-class');
12
item.classList.add('third-class');

Weitere Informationen finden Sie in den DOM-API-Dokumenten: .classList-Eigenschaft, .remove()-Methode

11. Toggle Class

jQuery:

1
/* Syntax */
2
3
.toggleClass();
1
/** Example

2
 * 

3
 * Adds the 'active' class to elements with 'my-class' if they don' have it

4
 * remove it if they have it

5
 * 

6
 */
7
 
8
$('.my-class').toggleClass('active');

Weitere Informationen finden Sie in den Dokumenten der jQuery-API: .toggleClass()-Methode

JavaScript:

1
/* Syntax */
2
3
Element.classList.toggle();
1
/**

2
 * Example 

3
 * 

4
 * For declaring the 'item' variable, see examples #3, #4, #5

5
 */
6
 
7
item.classList.toggle('active');

Weitere Informationen finden Sie in den DOM-API-Dokumenten: .classList-Eigenschaft, .toggle()-Methode

12. Has Class

jQuery:

1
/* Syntax */
2
3
.hasClass();
1
/**

2
 * Example

3
 * 

4
 * Checks if any element with 'my-class' has the 'active' class

5
 * 

6
 * Returns true or false

7
 * 

8
 * If there's at least one element with 'active' it's true,

9
 * otherwise false.

10
 * 

11
 */
12
13
$('.my-class').hasClass('active');

Weitere Informationen finden Sie in den Dokumenten der jQuery-API: .hasClass()-Methode

JavaScript:

1
/* Syntax */
2
3
Element.classList.contains();
1
/**

2
 * Example 

3
 * 

4
 * Similar to the jQuery version, this one also checks if any element

5
 * in the whole classList has the 'active' class

6
 * 

7
 * If at least one element has 'active', it's true, otherwise false

8
 * 

9
 * For declaring the 'item' variable, see examples #3, #4, #5

10
 */
11
 
12
item.classList.contains('active');

Weitere Informationen finden Sie in den DOM-API-Dokumenten: .classList-Eigenschaft, .contains()-Methode

13. Attribut abrufen

jQuery:

1
/* Syntax */
2
3
.attr('attr-name');
1
/**

2
 * Example

3
 * 

4
 * Returns the value of the href property of the FIRST occurrence of 

5
 * an element with 'my-class'

6
 * 

7
 */
8
 
9
$('.my-class').attr('href');

Weitere Informationen finden Sie in der jQuery-API-Dokumentation: .attr()-Methode

JavaScript:

1
/* Syntax */
2
3
Element.getAttribute('attr-name');
1
/**

2
 * Example 

3
 * 

4
 * For declaring the 'item' variable, see examples #3, #4, #5

5
 */
6
 
7
 item.getAttribute('href');

Weitere Informationen finden Sie in den DOM-API-Dokumenten: .getAttribute()-Methode

14. Attribut festlegen

jQuery:

1
/* Syntax */
2
3
.attr('attr-name', value);
1
/**

2
 * Example

3
 * 

4
 * Sets the value of the href property for ALL contact links that

5
 * have the 'contact-link' class

6
 * 

7
 */
8
 
9
$('.contact-link').attr('href', 'contact.html');

Weitere Informationen finden Sie in den Dokumenten zur jQuery-API: .attr()-Methode (Sie müssen dieselbe .attr()-Methode verwenden, um einen Attributwert abzurufen, jedoch mit zwei Parametern anstelle von einem).

JavaScript:

1
/* Syntax */
2
3
Element.setAttribute();
1
/**

2
 * Example 

3
 * 

4
 * For declaring the 'item' variable, see examples #3, #4, #5

5
 */
6
 
7
 item.setAttribute('href', 'contact.html');

Weitere Informationen finden Sie in den DOM-API-Dokumenten: .setAttribute()-Methode

15. Attribut entfernen

jQuery:

1
/* Syntax */
2
3
.removeAttr('attr-name');
1
/**

2
 * Example

3
 * 

4
 * Removes 'target' attributes from contact links

5
 */
6
 
7
$('.contact-link').removeAttr('target');

Weitere Informationen finden Sie in den Dokumenten der jQuery-API: .removeAttr()-Methode

JavaScript:

1
/* Syntax */
2
3
Element.removeAttribute();
1
/**

2
 * Example 

3
 * 

4
 * For declaring the 'item' variable, see examples #3, #4, #5

5
 */
6
 
7
 item.removeAttribute('target');

Weitere Informationen finden Sie in den DOM-API-Dokumenten: .removeAttribute()-Methode

16. HTML-Inhalt abrufen oder festlegen

jQuery:

1
/* Syntax */
2
3
.html();
4
.html('html-string');
1
/**

2
 * Example

3
 * 

4
 * (1) Gets the HTML content of the FIRST element that matches 'my-class'

5
 * 

6
 * (2) Sets/resets the HTML content of EACH element that matches 'my-class'

7
 * 

8
 */
9
 
10
$('.my-class').html();
11
12
$('.my-class').html('<em>Hello</em>');

Weitere Informationen finden Sie in der jQuery-API-Dokumentation: .html()-Methode

JavaScript:

1
/* Syntax */
2
3
Element.innerHTML;
1
/**

2
 * Example 

3
 * 

4
 * For declaring the 'item' variable, see examples #3, #4, #5

5
 */
6
 
7
item.innerHTML;                         // gets the value

8
item.innerHTML = '<em>Hello</em>';      // sets the value

Weitere Informationen finden Sie in den DOM-API-Dokumenten: .innerHTML-Eigenschaft

17. CSS-Eigenschaft abrufen oder festlegen

jQuery:

1
/* Syntax */
2
3
.css('property-name');
4
.css('property-name', value);
1
/**

2
 * Example

3
 * 

4
 * (1) Gets the 'color' value of the FIRST element

5
 * that has 'my-class'

6
 * 

7
 * (2) Sets the 'color' value to 'white' for EVERY element that has 

8
 * 'my-class'

9
 */
10
 
11
$('.my-class').css('color');
12
13
$('.my-class').css('color', 'white');

Weitere Informationen finden Sie in der jQuery-API-Dokumentation: .css()-Methode

JavaScript:

1
/* Syntax */
2
3
ElementCSSInlineStyle.style.{propertyName};
1
/**

2
 * Example 

3
 * 

4
 * For declaring the 'item' variable, see examples #3, #4, #5

5
 */
6
 
7
item.style.color;               // getting value

8
9
item.style.color = 'white';     // setting value

Weitere Informationen finden Sie in den DOM-API-Dokumenten: .style-Eigenschaft und CSS-Eigenschaftenreferenz (für die JavaScript-Namen der CSS-Eigenschaften).

18. Element ausblenden

jQuery:

1
/* Syntax */
2
3
.hide();
1
/**

2
 * Example

3
 * 

4
 * Hides all elements with 'my-class'

5
 */
6
 
7
$('.my-class').hide();

Weitere Informationen finden Sie in den Dokumenten der jQuery-API: .hide()-Methode

JavaScript:

1
/* Syntax */
2
3
ElementCSSInlineStyle.style.display = 'none';
1
/**

2
 * Example 

3
 * 

4
 * For declaring the 'item' variable, see examples #3, #4, #5

5
 */
6
 
7
item.style.display = 'none';

Weitere Informationen finden Sie in den DOM-API-Dokumenten: .style-Eigenschaft

19. Element anzeigen

jQuery:

1
/* Syntax */
2
3
.show();
1
/**

2
 * Example

3
 * 

4
 * Displays all elements with 'my-class'

5
 */
6
7
$('.my-class').show()

Weitere Informationen finden Sie in den Dokumenten der jQuery-API: .show()-Methode

JavaScript:

1
/* Syntax */
2
3
ElementCSSInlineStyle.style.display = '';
1
/**

2
 * Example  

3
 * 

4
 * For declaring the 'item' variable, see examples #3, #4, #5

5
 */
6
7
item.style.display = '';            // resets default

8
item.style.display = 'block';       // sets display as block

9
item.style.display = 'flex';        // sets display as flex

Weitere Informationen finden Sie in den DOM-API-Dokumenten: .style-Eigenschaft

20. Fügen Sie den Ereignis-Listener hinzu

jQuery:

1
/* Syntax */
2
3
.on();
1
/**

2
 * Example

3
 * 

4
 * Adds or removes the 'active' class to/from all elements with 

5
 * '.submenu' when #toggle is clicked

6
 */
7
 
8
$('#toggle').on('click', function(){
9
    $('.submenu').toggleClass('active');
10
});

Weitere Informationen finden Sie in der Methode jQuery API docs: .on()-Methode

JavaScript:

1
/* Syntax */
2
3
EventTarget.addEventListener('event', functionName);
1
/**

2
 * Example  

3
 * 

4
 * The code below only selects the FIRST element with the 

5
 * 'submenu' class.

6
 * 

7
 * To select all submenus, use the 'for' loop in Example #3 and #4

8
 */
9
10
let toggle = document.querySelector("#toggle");
11
let submenu = document.querySelector(".submenu");
12
13
toggle.addEventListener('click', function() {
14
   submenu.classList.toggle('active'); 
15
});

Sehen Sie Testcode:

Please accept marketing cookies to load this content.

Weitere Informationen finden Sie in den DOM-API-Dokumenten: .addEventListener()-Methode und DOM-Ereignisreferenz

Nächste Schritte

Dieser Spickzettel zu jQuery to JavaScript enthält die wichtigsten Funktionen, die Sie zum Erstellen einer Website benötigen. Wenn Sie sich mit diesen Eigenschaften und Methoden vertraut machen, können Sie von jQuery zu JavaScript springen. Sie müssen jedoch wahrscheinlich mehr wissen, wenn Sie komplexere Funktionen erstellen möchten.

Es gibt zwei weitere Ressourcen, die ich empfehlen würde, um das von Ihnen benötigte jQuery-zu-JavaScript-Äquivalent zu finden: die jQuery-Website, die Sie möglicherweise nicht benötigen, und Jack O’Connors jQuery-zu-JavaScript-Repo auf GitHub.

Obwohl ich die klassische for-Schleife verwendet habe, um querySelectorAll()-Matches zu durchlaufen, bietet Ihnen modernes JavaScript auch andere Optionen - Chris Coyier zeigt einige davon in diesem Artikel über JavaScript-Schleifen.

Das Experimentieren und Erstellen eigener Codefragmente kann auch sehr hilfreich sein, wenn Sie natives JavaScript und die DOM-API in Ihrem täglichen Workflow sicher verwenden möchten.

Weitere Informationen JavaScript mit Tuts+

Wenn Sie gerade erst mit JavaScript beginnen, bringen Sie diese Tuts+ -Kurse in die richtige Richtung: