- MooTools 1.3 Cookbook
- Jay Larry G. Johnston
- 254字
- 2021-04-02 19:07:08
Finding a group of elements by their class attribute
Collections of HTML elements may be handled as a group.
Getting ready
A favorite trick is to turn the option elements of a select on and off. Using various collections of elements, this is a snap in MooTools.
How to do it...
Use ID attributes to affect unique elements: $('my_single_target')
, and use class attributes to affect groups of elements: $$('.my_target_class')
.
<script type="text/javascript" src="mootools-1.3.0.js"></script> </head> <body> <form action="javascript:" method="get"> Choose an Animal:<br/> <select id="animal"> <option value="">Select Animal</option> <option value="cat">Cat</option> <option value="dog">Dog</option> <option value="pig">Pig</option> </select> // some browsers do not fire select onchange // until focus changes <input type="button" value="Select Animal"/> <br/><br/> <select id="animal_items"> <!-- cat options --> <option class="nopeek cat" value="1">Ball of String</option> <option class="nopeek cat" value="2">Fake Mouse</option> <option class="nopeek cat" value="3">Catnip (mmm)</option> <!-- dog options --> <option class="nopeek dog" value="4">Juicy Bone</option> <option class="nopeek dog" value="5">Bouncy Ball</option> <option class="nopeek dog" value="6">Rope w/ Handle</option> <!-- pig options --> <option class="nopeek pig" value="7">Mud</option> <option class="nopeek pig" value="8">Slop</option> <option class="nopeek pig" value="9">Water Hose</option> </select> </form> <noscript>JavaScript is disabled.</noscript> <script type="text/javascript"> // get a collection of elements so we can hide them onchange var all = $$('.nopeek'); all.setStyle('display','none'); $('animal').addEvent('change',function pare_options() { all.setStyle('display','none'); var animal_chosen = $('animal').get('value'); $$('.'+animal_chosen).setStyle('display','block'); }); </script>
How it works...
Our first select has a group of choices where each value represents a class that is given to groups of option elements in the second drop-down menu. When the first widget is changed, the onChange event calls the bound anonymous function and uses the class all
to turn off the visibility of all option elements then uses the class value chosen to pare down the selection options.
推薦閱讀
- 中文版SketchUp 2022完全實(shí)戰(zhàn)技術(shù)手冊(cè)
- 3ds Max 2014標(biāo)準(zhǔn)教程(全視頻微課版)
- 創(chuàng)意UI:Photoshop玩轉(zhuǎn)移動(dòng)UI設(shè)計(jì)
- AJAX and PHP: Building Modern Web Applications 2nd Edition
- UG NX 8.0基礎(chǔ)與實(shí)例教程
- Inkscape 0.48 Illustrator's Cookbook
- Photoshop CS6從入門到精通
- 畫筆圣經(jīng) Photoshop專業(yè)繪畫技法
- Oracle Siebel CRM 8 User Management: LITE
- RESTful PHP Web Services
- 大規(guī)模組織DevOps實(shí)踐
- Premiere CC視頻編輯入門與應(yīng)用
- The PEAR Installer Manifesto
- 會(huì)聲會(huì)影視頻編輯實(shí)戰(zhàn)秘技250招
- Quality Assurance for Dynamics AX-Based ERP Solutions