130 likes | 211 Views
jQuery Part 2. Creating HTML Elements. Create element with HTML var txt1=“<p>Text.</p>â€; Create element with jQuery var txt2=$(“<p></p>â€).text(“Text.â€); Create element with DOM var txt3= document.createElement (“pâ€); txt3.innerHTML(“Text.â€);. DOM Manipulation.
E N D
Creating HTML Elements • Create element with HTML • var txt1=“<p>Text.</p>”; • Create element with jQuery • var txt2=$(“<p></p>”).text(“Text.”); • Create element with DOM • var txt3=document.createElement(“p”);txt3.innerHTML(“Text.”);
DOM Manipulation • Getting the content of an element • text() – Returns the text content of selected elements • $(“div”).text() • html() – Returns the content of selected elements • $(“div”).html() • val() – Returns or sets the value from form fields • $(“#fname”).val() • attr() – Returns or sets the attribute value • $(“#foo”).attr(“href”)
DOM Manipulation • Setting the content of an element • text(“value”) – Sets the text content of selected elements • $(“#foo”).text(“bar”) • text(function(i,origText){…}) • html(“value”) – Sets the content of selected elements • $(“#foo).html(“<b>bar</b>”) • html(function(i,origHtml){…}) • val(“value”) – Sets the value from form fields • $(“#fname”).val(“Cam”) • val(function(i,origVal){…}) • attr(“attr”,“value”) – Sets the attribute value • $(“#foo”).attr(“href”, “http://example.com”) • attr(“attr”,function(i,origVal){…})
Adding Elements • append() – inserts content at the end of selected elements • prepend() – inserts content at the beginning of selected elements • after() – inserts content after the selected elements • before() – inserts content before the selected elements • All four methods support multiple elements • $(“img”).after(txt1,txt2,txt3);
Removing Elements • remove() – removes the selected elements and their children • $(“#foo”).remove(); • remove() supports filters. Filters are selectors • $(“#foo”).remove(“.italic”) • empty() – removes the child elements of the selected elements • $(“#foo”).empty();
Manipulating the CSS • addClass() – Adds one or more classes to the selected elements • $(“#foo”).addClass(“important blue”); • removeClass() – Removes one or more classes from the selected elements • $(“#foo”).removeClass(“blue important”); • toggleClass() – Toggles one or more classes from the selected elements • $(“#foo”).toggleClass(“blue”); • css() – Returns or sets the style attribute • $(“#foo”).css(“property-name”) • $(“#foo”).css(“property-name”,”value”)
jQuery Dimensions Margin Border Padding Element height() innerHeight() outerHeight() outerHeight(true) width() innerWidth() outerWidth() outerWidth(true)
Dimensions • width() – Returns or sets the width of selected element • height() – Returns or sets the height of selected element • innerWidth() – Returns the width with padding of selected element • innerHeight() – Returns the height with padding of selected element • outerWidth() – Returns the width with padding and border • outerHeight() – Returns the height with padding and border • outerWidth(true) – Returns the width with padding, border and margin • outerHeight(true) – Returns the height with padding, border and margin • $(document).height() • $(window).width()
Traversing up the DOM • parent() – Returns the direct parent of the selected element • parents() – Returns all ancestor elements all the way to <html> • parentsUntil(selector) – Returns all ancestor elements between selected element and selector
Traversing down the DOM • children() – Returns all the direct children of the selected element • find(selector) – Returns descendant elements matching selector of the selected element
Traversing Sideways • siblings() – Returns all sibling elements • Takes optional filter • next() – Returns the next sibling element • nextAll() – Returns all the next siblings • nextUntil(selector) – Returns next siblings between selected and selector • prev(), prevAll(), prevUntil() – are similar, but return previous siblings
Filtering Selections • first() – Returns the first element in the selection • $(“div p”).first(); • last() – Returns the last element in the selection • $(“div p”).last(); • eq(num) – Returns the element at the given index. • $(“div p”).eq(1); • filter() – Removes the elements that do not match the selection criteria from the list • $(“div p”).filter(“.intro”); • not() – Returns all elements that do not match the selection criteria • $(“div p”).not(“.intro”)