tailieunhanh - Tự học HTML và CSS trong 1 giờ - part 48

Tham khảo tài liệu 'tự học html và css trong 1 giờ - part 48', công nghệ thông tin, đồ họa - thiết kế - flash phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | 446 LESSON 15 Using JavaScript in Your Pages FIGURE The FAQ page with the JavaScript included. Here s the JavaScript contained in the file function var faqList answers questionLinks questions currentNode i j faqList faq answers dd for i 0 i i answers i . none1 questions dt for i 0 i i questions i .onclick function currentNode while currentNode if 1 DD if none block else none break currentNode return false Download from Hiding and Showing Elements 447 This JavaScript code is significantly more complex than any used previously in the book. Take a look at the first line which is repeated here function This is where the unobtrusiveness comes in. Instead of calling a function using the 15 onload attribute of the body tag to start up the JavaScript for the page I assign an anonymous function to the onload property of the window object. The code inside the function will run when the onload event for the window is fired by the browser. Setting up my JavaScript this way allows me to include this JavaScript on any page without modifying the markup to bind it to an event. That s handled here. This is the method for binding functions to events programmatically. Each element has properties for the events it supports to bind an event handler to them you assign the function to that property. You can do so by declaring an anonymous function in the assignment statement as I did in this example or you can assign the function by name like this function doStuff Does stuff doStuff In this case I intentionally left the parentheses out when I used the function name. That s because I m assigning the function itself to the onload property