1 / 27

Review jQuery (jsfiddle/suX84/3 /)

Review jQuery (http://jsfiddle.net/suX84/3 /). Today’s Outline. Other jQuery Functions DOM Traversal Event Objects. Goal Today: Practice jQuery & Learning the DOM. Other jQuery Functions. Other jQuery Functions. . addClass (‘ className ’) Ex: <div class=“title”>Title</div>

theo
Download Presentation

Review jQuery (jsfiddle/suX84/3 /)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Review jQuery (http://jsfiddle.net/suX84/3/)

  2. Today’s Outline Other jQuery Functions DOM Traversal Event Objects

  3. Goal Today: Practice jQuery & Learning the DOM

  4. Other jQuery Functions

  5. Other jQuery Functions • .addClass(‘className’) • Ex: <div class=“title”>Title</div> • $(‘.title’).addClass(‘superbold’); • <div class=“title superbold”>Title</div> • .removeClass(‘className’) • .hasClass(‘className’) • Ex: <div class=“title superbold”>Title</div> • if($(‘.title’).hasClass(‘superbold’)) { alert(‘Title is super bolded!!’);}

  6. Other jQuery Functions • .attr(‘…’) • Ex: <a id=“link1” href=“http://www.google.com”>Google</a> • var sample = $(‘#link1’).attr(‘href’);alert(sample); • .attr(‘…’) works for any attribute • Ex: .attr(‘href’) • Userful ones: • .attr(‘id’) • .attr(‘class’) • .attr(‘width’) // For <img tags> • .attr(‘src’)

  7. Other jQuery Functions • Setting .attr() • Syntax: • .attr(‘src’, ‘images/newimg.png’) • .attr(‘href’, ‘www.yahoo.com’) • Takes 2 arguments • 1st is the attribute name • 2nd is the attribute value

  8. Demo Follow along: jsfiddle.net/5pye4/1/

  9. DOM Traversal

  10. DOM Traversal • What is the DOM? • Document Object Model • A way to structure a document & represent hierarchy of elements • E.x. <div id=“box”> <div id=“title”>Title</div> • </div> • In above, #title is a child of #box (parent-child relationship) • What is DOM Traversal? • Accessing different elements in the DOM and utilizing their heirarchy

  11. DOM Traversal <html> <body> <head> <div> <div> <title> <div> <ul> <li> <li> <li>

  12. DOM Traversal <div id=“box”> <div id=“title”> Title </div> <div id=“subtitle”> Subtitle </div> <div id=“content”> Content </div> </div> #box parentNode firstChild #title nextSibling CHILDREN #subtitle SIBLINGS previousSibling lastChild #content

  13. DOM Traversal - jQuery <div id=“box”> <div id=“title”> Title </div> <div id=“subtitle”> Subtitle </div> <div id=“content”> Content </div> </div> #box .parent() .children().first() #title .next() .children() #subtitle .siblings() .prev() .children().last() #content

  14. DOM Traversal - Example Reference .children() .children().first() .children().last() .siblings() .prev() .next() .parent() You are given $(‘#menu’). How to select Pizza element? • <ul id=”menu”> • <li>Tacos</li> • <li>Pad Thai</li> • <li>Subway</li> • <li>Pizza</li> • </ul> Answer: $(‘#menu’).children().last() You are given $(‘#menu’). How to select Pad Thai? Answer: $(‘#menu’).children().first().next()

  15. DOM Traversal - Searching #parentelm • What about pages with zillions of elements? • For Parent, Grandparent, etc • .closest(‘#parentelm’) #box • For Child, Grandchild, etc. • .find(‘#childelm’) #childelm

  16. DOM Traversal - Example • <div id=”home-panel”> • <div class=”container”> • <div class=”row”> • <div class=”span12”> • <div class=”title”> • My Name • </div> • </div> • </div> • </div> • </div> This is a sample Twitter Bootstrap structure. How to access “My Name” element using $(‘#home-panel’)? $(‘#home-panel’).find(‘.title’)

  17. DOM Traversal – Use Cases <div class=“modal”> <div class=“x-button”>X</div> <div class=“content”> This is a Modal! </div> </div> Example 1: $(‘.x-button’).click(function() { }); $(this).parent().fadeOut();

  18. DOM Traversal – Use Cases <div class=“panels”> <div id=“home” class=“panel”> … </div> <div id=“about” class=“panel”> … </div> </div> <a href=“#home”> <div class=“button”>Home</div> </a> <a href=“#about”> <div class=“button”>About</div> </a> Example 2: In CSS: .panel { display: none; } $(‘.button’).click(function() { }); var elm = $(this).parent().attr(‘href’); $(‘.panels’).find(elm).fadeIn();

  19. Demo Follow along: jsfiddle.net/6QS7J/

  20. Event Objects

  21. Event Objects What are Event Objects? $(‘.button’).click(function(event) { …. }); They give you special functions to use. Such as getting the X and Y value of when you clicked ‘.button’! $(‘.button’).click(function(event) { alert(event.pageX + “ and “ + event.pageY); });

  22. Event Objects – preventDefault() We will go over 2 important special functions. First is preventDefault() <a id=“button” href=“www.google.com”> Google </a> $(‘#button’).click(function(event) { event.preventDefault(); }); Now when you click on #button, the browser won’t take you to Google. It prevents the default action of <a> tags, which takes you to a new page.

  23. Event Objects – stopPropagation() The second special function is stopPropagation() <div id=“box1”> <div id=“box2”> Click Here! </div> </div> $(‘#box1’).click(function() { $(‘#box2’).css(‘background’, ‘red’); }); $(‘#box2’).click(function(event) { event.stopPropagation(); $(‘#box2’).css(‘background’, ‘red’); }); Instead of doing #box2’s click event and#box1’s click event, we now only do #box2’s. .stopPropagation() stops an event from calling parent’s events

  24. Event Objects – stopPropagation() Last week’s modal example using stopPropagation() <div id=“black”> <div id=“modal”> This is a Modal! </div> </div> $(‘#black’).click(function() { $(this).fadeOut(); }); $(‘#modal’).click(function(event) { event.stopPropagation(); }); Because #modal is inside #black, by default when you click #modal it will trigger #black’s click event. To prevent this, we added a stopPropagation to #modal.

  25. Event Objects Lastly, note event could be whatever you want to take the variable. event or just e is usually the industry standard. <div id=“black”> <div id=“modal”> This is a Modal! </div> </div> $(‘#black’).click(function() { $(this).fadeOut(); }); $(‘#modal’).click(function(berkeley) { berkeley.stopPropagation(); });

  26. Summary • jQuery functions • .addClass(), .attr(…) • DOM Traversal • Usage of .child(), .parent(), etc. • .find() and .closest() • Event Objects • .preventDefault() • .stopPropagation() All lecture material, handouts, and homework can be found at: http://www.thewebdesignworkshop.co

More Related