jQuery ist ein JavaScript-Framework.
jQuery(document).ready(function($) { // jQuery code goes here ... });
$('#foo').append('<div>Hello World</div>');
// Element kopieren var foo = $('.foo').clone(); // Element wieder im DOM einfügen $('.bar').append(foo);
$("#foo").text("Lorem ipsum");
$("#foo").html("Lorem <em>ipsum</em>");
$('#foo').css('color', 'red');
jQuery API: Toggle
Die toggle
-Funktion als Event wurde als jQuery (ab Version 1.9) entfernt.
var elementID = $('#foo').attr('id');
$(this).parent().attr('id');
Bestimmte, untergeordnete Elemente (bspw. bestimmte Klassen) von this
lassen sich über folgende zwei Wege selektieren (beide Arten sind equivalent):
$(".class", this);
$(this).find(".class");
Wenn die Elemente direkte Kinder von this
sind, kommt auf folgende Möglichkeit in Frage:
$(this).children(".class");
Quelle: Stack Overflow
var listItemsCount = $('ul > li').length;
Quelle: Stack Overflow
var currentMousePos = { x: -1, y: -1 }; $(document).mousemove(function(event) { currentMousePos.x = event.pageX; currentMousePos.y = event.pageY; });
Quelle: How to get mouse position in jQuery without mouse events – Stack Overflow
$("#foo").attr("src","foobar.jpg");
$("img#foo").hover( function() { this.src = this.src("image2.jpg"; }, function() { this.src = this.src("image1.jpg"); } );
$('a[href^="#"]').on('click', function(e) { // Prevent default anchor click behavior e.preventDefault(); // Store hash var hash = this.hash; // Animate $('html, body').animate({ scrollTop: $(hash).offset().top }, 300, function(){ // when done, add hash to url (default click behaviour) window.location.hash = hash; }); });
Quelle: Stack Overflow: How to add smooth scrolling to Bootstrap's scroll spy function?