TAILIEUCHUNG - JQuery: Novice to Ninja- P26
JQuery: Novice to Ninja- P26:No matter what kind of ninja you are—a cooking ninja, a corporate lawyer ninja, or an actual ninja ninja—virtuosity lies in first mastering the basic tools of the trade. Once conquered, it’s then up to the full-fledged ninja to apply that knowledge in creative and inventive ways. | 352 jQuery Novice to Ninja chapter_09 11_custom_events excerpt disclaimer .bind do-toggle function this .toggle slow We have bound our custom do-toggle event to the disclaimer element. When the do-toggle event fires the function will run and the disclaimer will hide or show itself. But how do we fire the do-toggle event By using the trigger method chapter_09 11_custom_events excerpt toggleButton .click function disclaimer .trigger do-toggle When the button is clicked the disclaimer is toggled. It might seem like the long way round compared to just hiding and showing with the toggle button but now we ve done two very important tasks we ve moved the code responsibility to the element itself and given ourselves the ability to fire the event from any other location. Whether we want to toggle the disclaimer via our toggle button or via a small icon at the top of the screen there s no need to replicate the code we just fire the do-toggle event from wherever we like. But wait there s more The bind trigger system also lets you append parameters when you fire events. Each triggering element can pass different data so the event handler can customize its response. This makes custom events very powerful you can now create widgets that can elegantly be controlled by multiple other elements on the page so you can cleanly separate and isolate page behavior and make your code far more reusable. As an example we re going to put an alternate trigger mechanism on the page for our animated content panes from the section called Bouncy Content Panes in Chapter 3. In this example we made cool-looking content panes showing celebrity biographies that bounced open and closed when their headings were clicked. For clarity here we ll omit the bouncing effect so when the user clicks on the biography headings the panes will toggle instantly. Plugins Themes and Advanced Topics 353 However there ll also be a select box above the panes. When a celebrity is chosen from the list .
đang nạp các trang xem trước