Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dynamic HTML. A combination of technologies to create dynamic web pages – xhtml – CSS – Javascript Browsers make the page that is being displayed, its.

Similar presentations


Presentation on theme: "Dynamic HTML. A combination of technologies to create dynamic web pages – xhtml – CSS – Javascript Browsers make the page that is being displayed, its."— Presentation transcript:

1 Dynamic HTML

2 A combination of technologies to create dynamic web pages – xhtml – CSS – Javascript Browsers make the page that is being displayed, its style properties, and events accessible to JavaScript.

3 The DOM A platform- and language-neutral interface that allows programs and scripts to dynamically access and update the content and structure of HTML and XHTML documents every element in the HTML document is represented by an object Elements can be manipulated using the properties and methods of the corresponding objects Changes in the element properties are immediately reflected by the browser

4 The DOM

5 DOM Standards W3C www.w3.org defines the standards www.w3.org DOM Level 3 recommendation – www.w3.org/TR/DOM-Level-3-Core/ www.w3.org/TR/DOM-Level-3-Core/ DOM Level 2 HTML Specification – www.w3.org/TR/DOM-Level-2-HTML/ www.w3.org/TR/DOM-Level-2-HTML/ –additional DOM functionality specific to HTML, in particular objects for XHTML elements But, the developers of web browsers –don't implement all standards –implement some standards differently –implement some additional features

6 Accessing HTML Elements All HTML elements (objects) are accessed through the document object document itself is automatically created Several ways to access a specific element – paths in the DOM tree – retrieval by tag – retrieval by ID

7 Accessing Elements by Tags This example is lovely. But this one is even more! function execute() { var spans = document.getElementsByTagName("span"); spans[0].style.color="red"; spans[1].style.color="blue"; spans[1].style.fontVariant="small-caps"; } body head

8 Accessing Elements by ID This text can be hidden! function execute() { var theDiv = document.getElementById("div1"); if (theDiv.style.visibility=="hidden") {theDiv.style.visibility="visible" } else {theDiv.style.visibility="hidden" } } head This technique is more stable w.r.t. document changes (why?) body

9 Element Properties Elements of different types have different sets of properties and methods See www.w3schools.com/htmldom/ for a detailed list of element properties and methodswww.w3schools.com/htmldom Most elements have the style member style is an object that represents the style- sheet rules applied over the element

10 Other Node Properties nodeType property ELEMENT_NODE : HTML element TEXT_NODE : text within a parent element ATTRIBUTE_NODE : an attribute of a parent element –attributes can be accessed another way CDATA_SECTION_NODE –CDATA sections are good for unformatted text – nodeName property – nodeValue property – attributes property – innerHTML property not standard, but implemented in major browsers very useful – style property object whose properties are all style attributes, e.g., those defied in CSS

11 The innerHTML Property The attribute innerHTML attribute of an element is the HTML code embedded inside that element Hence, you can replace existing content by setting this attribute: – element.innerHTML = "new HTML code” function replace(button) { d = document.getElementById("d1"); d.innerHTML = " This is a header "; button.disabled=true; }

12 Special DOM Objects window –the browser window –new popup window s can be opened document –the current web page inside the window body – element of the document history –sites that the user visited –makes it possible to go back and forth using scripts location –URL of the document –setting it goes to another page

13 Events

14 Event Example Simple Events function focusInput() { var theInput = document.getElementsByTagName("input")[0] theInput.style.background="yellow" } function blurInput() { theInput = document.getElementsByTagName("input")[0] theInput.style.background="white" }

15 Event Example (cont) <img src="lighton.gif" alt="light bulb" onmouseover="alert('Mouse Over')" /> <input type="text" onfocus="focusInput()" onblur="blurInput()" />

16 Event Model Events usually occur due to users actions – For example, pressing the keyboard, changing a text field, moving the mouse over an element, etc. An event is represented by an event object that is created upon the event occurrence Every event has an associated target element – For example, the image over which the mouse clicks

17 Event Model (cont) Elements can have registered event listeners which are associated with certain types of events When an event takes place, the listeners that are registered for this event are invoked Typically, a listener is described by a scripting code (e.g., JavaScript) – This code is executed upon listener invocation

18 Inline Listener Registration The simplest (and most common) way to register a listener is by an attribute assignment: ontype = "JavaScript code" For example: The JavaScript code has access to the following objects: – this - the element (e.g., the image defined above) – event - the event object

19 Some Event Types load unload abort click dblclick mousedown mousemove mouseup mouseover reset select submit change blur focus keydown keypress keyup

20 Another Example Event Object Example function execute(e) { alert(" x: " + e.clientX + ", y: " + e.clientY + " mouse button: " + e.button); } <body onmousedown="execute(event)" style="cursor: pointer; position:absolute; width:100%; height:100%">

21 JQuery

22 Open source library for manipulating the DOM JQuery object has functions for – Selecting DOM elements – Manipulating DOM elements – Event handling – Effects and Animations – AJAX – Utilities Simple way into dynamic HTML

23 jQuery Object jQuery – Most often seen as $ $ is a legal identifier in JavaScript – Better, cleaner to use jQuery Can be used as a function and as an object – JQuery function is used to select DOM elements – jQuery object is used for utility functions Includes a way to define a function that will run when the page is fully loaded

24 jQuery document ready Function that is called when page is loaded jQuery(document).ready(function() { // Javascript }); Can be placed anywhere

25 jQuery Hello World function tryMe() { jQuery("#buttondiv").html("You pushed my button"); } $(document).ready(function(){ $("#msgid").html("This is Hello World by JQuery"); }); This is Hello World by HTML Try Me

26 jQuery Selector Select DOM elements by – HTML element tagjQuery(“p”) – HTML element classjQuery(“.class”) – HTML element idjQuery(“#id”) – More advanced selectors Selector returns the elements as a JavaScript object – Methods for manipulating the elements

27 jQuery Selector $(selector) selector: – $( ‘ #id ’ )id of element – $( ‘ p ’ )tag name – $( ‘.class ’ )CSS class – $( ‘ p.class ’ ) elements having the CSS class – $( ‘ p:first ’ )$( ‘ p:last ’ )$( ‘ p:odd ’ )$( ‘ p:even ’ ) – $( ‘ p:eq(2) ’ )gets the 2 nd element (1 based) – $( ‘ p ’ )[1]gets the 2 nd element (0 based) – $( ‘ p:nth-child(3))gets the 3 rd element of the parent. n=even, odd too. – $( ‘ p:nth-child(5n+1) ’ )gets the 1 st element after every 5th one – $( ‘ p a ’ ) elements, descended from a – $( ‘ p>a ’ ) elements, direct child of a – $( ‘ p+a ’ ) elements, directly following a – $( ‘ p, a ’ ) and elements – $( ‘ li:has(ul) ’ ) elements that have at least one descendent – $( ‘ :not(p) ’ )all elements but elements – $( ‘ p:hidden ’ )only elements that are hidden – $( ‘ p:empty ’ ) elements that have no child elements

28 Useful jQuery Functions.each()iterate over the set.size()number of elements in set.end()reverts to the previous set.get(n)get just the nth element (0 based).eq(n)get just the nth element (0 based) also.lt(n) &.gt(n).slice(n,m)gets only nth to (m-1)th elements.not( ‘ p ’ )don ’ t include ‘ p ’ elements in set.add( ‘ p ’ )add elements to set.remove() removes all the elements from the page DOM.empty()removes the contents of all the elements.filter(fn/sel)selects elements where the func returns true or sel.find(selector)selects elements meeting the selector criteria.parent()returns the parent of each element in set.children()returns all the children of each element in set.next()gets next element of each element in set.prev()gets previous element of each element in set.siblings()gets all the siblings of the current element

29 Other Functions Formatting Functions –.css(property, value) –.html() –.val()(form elements) –.text() –.addClass( ‘ class ’ ) –.removeClass( ‘ class ’ ) Adding Elements – $( ‘ #target ’ ).before( ‘ Inserted before #target ’ ); – $( ‘ #target ’ ).after( ‘ This is added after #target ’ ); – $( ‘ #target ’ ).append( ‘ Goes inside #target, at end ’ ); – $( ‘ #target ’ ).wrap( ‘ ’ );

30 Example 1 Compare the following: What are the advantages of the jQuery method? $("a").click(function(){ alert("You clicked a link!"); }); $("a").click(function(){ alert("You clicked a link!"); }); Link

31 Example 2 $("h2").click(function(){ $(this).hide("slow"); }); What will this do? What happens if you have more than one h2? Try it! What will this do? What happens if you have more than one h2? Try it!

32 Example 3 $("#btnHideBlue").click(function(){ $("p.blue").hide("slow"); }); Hide all blue paragraphs when btnHideBlue is clicked Hide Blue

33 jQuery Events For a full jQuery event reference, please see jQuery Events ReferencejQuery Events Reference

34 Example 4 $("#lemon").mouseover(function(){ $(this).append(" Cookie! "); }); Append text to paragraph lemon on mouseover Lemon drops biscuit chocolate…

35 Manipulating CSS For a full jQuery CSS reference, please see jQuery CSS Methods ReferencejQuery CSS Methods Reference For more on the css function, see http://api.jquery.com/css/ http://api.jquery.com/css/

36 Example 5 $("#btnColor").click(function(){ $("#lemon").addClass("blue"); }); Change color of paragraph lemon when btnColor is clicked.red{ color:red; }.blue{ color:blue; }.red{ color:red; }.blue{ color:blue; }

37 Example 6 Display the color of the paragraph lemon when btnColorCheck is clicked. What color is the paragraph? $("#btnColorCheck").click(function(){ alert($("#lemon").css("color")); });

38 Example 7 $("p").dblclick(function(){ $(this).css("background-color", "yellow"); }); Highlight (background-color = yellow) any paragraph that is double-clicked

39 jQuery Effects

40 Example 8 $("#btnToggle").click(function(){ $("#lemon").slideToggle("slow"); }); Create a toggle button that shows/hides paragraph lemon. Create a toggle button that shows/hides paragraph lemon.

41 Example 9 $("#btnFade").click(function(){ $("#lemon").fadeTo("slow", 0.5); }); Fade paragraph lemon to 50% opacity when btnFade is clicked.

42 Manipulating HTML For a full jQuery HTML reference, please see jQuery HTML Methods ReferencejQuery HTML Methods Reference

43 Example 10 $("#btnReplace").click(function(){ $("#lemon").html("Lollipop soufflé ice cream tootsie roll donut..."); }); Replace text in paragraph lemon when btnReplace is clicked.


Download ppt "Dynamic HTML. A combination of technologies to create dynamic web pages – xhtml – CSS – Javascript Browsers make the page that is being displayed, its."

Similar presentations


Ads by Google