Web Technologies and Programming Lecture 18
Dialog boxes in JavaScript
Alert box
Prompt box
Confirm box
What is DOM?
HTML DOM
Retrieving HTML elements
getElementById()
innerHTML
getAttribute()
setAttribute()
HTML DOM Elements
Finding HTML Elements
Find an element by element
Find elements by tag
Find elements by class name
46 trang |
Chia sẻ: hoant3298 | Lượt xem: 675 | Lượt tải: 0
Bạn đang xem trước 20 trang tài liệu Web Technologies and Programming Lecture 18, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
1Web Technologies and ProgrammingLecture 182Summary of Previous LectureWhat is JavaScript?Embedding JavaScript with HTMLJavaScript conventionsVariables in JavaScriptJavaScript operatorsInput output in JavaScriptJavaScript functionsConditional StatementsLooping Statements3Today’s Lecture OutlineDialog boxes in JavaScriptHTML Document Object Model (DOM)41. Dialog boxes in JavaScriptJavaScript provides the ability to pickup user input or display small amounts of text to the user by using dialog boxes These dialog boxes appear as separate windows and their content depends on the information provided by the user51. Dialog boxes in JavaScriptJavaScript has three kind of popup boxes: Alert boxPrompt boxConfirm box61.1 Alert BoxAn alert box is simply a small message box that pops up and gives the user some informationAn alert dialog box is mostly used to give a warning message to the usersWhen an alert box pops up, the user will have to click "OK" to proceedSyntax:alert(“message”)71.1 Alert BoxStart of FunctionDisplays an Alert BoxCalling the Function81.1 Alert BoxAlert Box91.2 Prompt boxA prompt box is often used if you want the user to input a value before entering a pageWhen a prompt box pops up, the user will have to click either "OK" or "Cancel" to proceed after entering an input valueIf the user clicks "OK" the box returns the input valueIf the user clicks "Cancel" the box returns null101.2. Prompt box11Start of the functionAsks user to enter nameWrites name on the webpageCalling a function1.2. Prompt box12Prompt BoxOutput1.3 Confirm boxA confirm box is often used if you want the user to verify or accept somethingWhen a confirm box pops up, the user will have to click either "OK" or "Cancel" to proceedIf the user clicks "OK", the box returns trueIf the user clicks "Cancel", the box returns false131.3 Confirm boxUser InputConfirmationNot Confirmed141.3 Confirm box152. Document Object Model (DOM)What is the DOM?The DOM is a W3C (World Wide Web Consortium) standard.The DOM defines a standard for accessing documents:"The W3C Document Object Model (DOM) is a platform and language-neutral interface that allows programs and scripts to dynamically access and update the content, structure, and style of a document.”162. Document Object Model (DOM)The W3C DOM standard is separated into 3 different parts:Core DOM - standard model for all document typesXML DOM - standard model for XML documentsHTML DOM - standard model for HTML documents172. HTML Document Object Model (DOM)Once html element are rendered (painted) in the browser window, browser can not recognize themTo create interactive web pages it is vital that the browser continues to recognize HTML elementsJavaScript enabled browsers do this because they recognize and uses DOM 182. HTML Document Object Model (DOM)The HTML DOM defines a standard set of objects for HTML, and a standard way to access and manipulate HTML documentsAll HTML elements, along with their containing text and attributes, can be accessed through the DOMThe contents can be modified or deleted, and new elements can be created192. HTML Document Object Model (DOM)The HTML DOM is platform and language IndependentIt can be used by any programming language like Java, JavaScript, and VBScriptThe HTML DOM can be thought of as a hierarchy moving from the most general object to the most specific202. Document Object Model (DOM)21WindowNavigatorDocumentHistoryLocationForms[ ]Array of formslinks[]Array of linksImages[]Array of imagesanchors[]Array of anchorsElement[ ] Array of element of formsTextboxes buttons select listResetfilesOptions[]li2. Document Object Model (DOM)When a web page is loaded, the browser creates a Document Object Model of the page.With the object model, JavaScript gets all the power it needs to create dynamic HTML:JavaScript can change all the HTML elements in the pageJavaScript can change all the HTML attributes in the pageJavaScript can change all the CSS styles in the pageJavaScript can remove existing HTML elements and attributes222. Document Object Model (DOM)JavaScript can add new HTML elements and attributesJavaScript can react to all existing HTML events in the pageJavaScript can create new HTML events in the page232. Html Document Object Model (DOM)HTML DOM methods are actions you can perform (on HTML Elements).HTML DOM properties are values (of HTML Elements) that you can set or change.In the DOM, all HTML elements are defined as objects.The programming interface is the properties and methods of each object.A property is a value that you can get or set (like changing the content of an HTML element).A method is an action you can do (like add or deleting an HTML element).242. Document Object Model (DOM)document.forms[0].elements[0].valuedocument.images[0].srcdocument.links[0].href252.1 Retrieving HTML elementsThe getElementById() method is a workhorse method of the DOM It retrieves a specified element of the HTML document and returns a reference to itTo retrieve an element, it must have an unique iddocument.getElementById(“element-id”)262.1 Retrieving HTML elementsThe returned reference can be used to retrieve element attributesdocument.getElementById(“element-id”).attributedocument.getElementById(“pic”).srcdocument.getElementById(“pic”).height272.1 Retrieving HTML elements28Function StartsGetting Reference to “pic”Accessing AttributesImage TagSetting idCalling Function2.1 Retrieving HTML elementsImageTitle AlertHeight Alert292.2 Retrieving the text of an elementinnerHTML property defines both the HTML code and the text that occurs between that element's opening and closingdocument.getElementById(“element-id”).innerHTML302.2 Retrieving the text of an element31Paragraph idCalling FunctionParagraph TextGetting Element ReferenceGetting Text2.2 Retrieving the text of an elementParagraph TextText Alert322.3 Getting value of attributesgetAttribute() method is used to retrieve values of attributesdocument.getElementById(“element-id”).getAttribute(“attribute-name”)document.getElementById(“pic”).getAttribute(“src”)332.3 Getting value of attributes34Getting Element ReferenceUsing getAttribute() Function2.3 Getting value of attributesImageTitle Attribute Alert352.4 Setting value of attributessetAttribute() method is used to set values of attributesdocument.getElementById(“element-id”).setAttribute(“attribute-name”, ”Value”)document.getElementById(“pic”).setAttribute(“src”, ”abc.jpg”)362.4 Setting value of attributes37Getting Reference ElementChanging Attribute Values2.4 Setting value of attributesBefore Setting Attribute ValuesAfter Setting Attribute Values383.0 HTML DOM ElementsFinding HTML ElementsTo do so, you have to find the elements first. There are a couple of ways to do this:Finding HTML elements by idFinding HTML elements by tag nameFinding HTML elements by class nameFinding HTML elements by CSS selectorsFinding HTML elements by HTML object collections393.1 HTML DOM ElementsFinding HTML Element by IdThe easiest way to find an HTML element in the DOM, is by using the element id.This example finds the element with id="intro":Example: var myElement = document.getElementById("intro");403.2 HTML DOM ElementsFinding HTML Elements by Tag NameThis example finds all elements:Examplevar x = document.getElementsByTagName("p");413.3 HTML DOM ElementsFinding HTML Elements by Class NameIf you want to find all HTML elements with the same class name, use getElementsByClassName().This example returns a list of all elements with class="intro".Examplevar x = document.getElementsByClassName("intro");423.4 HTML DOM ElementsFinding HTML ElementsMethodsDescriptiondocument.getElementById(id) Find an element by elementiddocument.getElementsByTagName(name)Find elements by tag namedocument.getElementsByClassName(name)Find elements by class name43Summary of Today’s LectureDialog boxes in JavaScriptAlert boxPrompt boxConfirm boxWhat is DOM? HTML DOMRetrieving HTML elementsgetElementById()innerHTMLgetAttribute()setAttribute()44Summary of Today’s LectureHTML DOM ElementsFinding HTML ElementsFind an element by elementFind elements by tag Find elements by class name45THANK YOU46
Các file đính kèm theo tài liệu này:
- lecture_18_wt_javascript_dom_2947_2028580.pptx