Now if I want to hide all account
blocks I execute
Element.hide('account_1_info')
开发者_开发百科Element.hide('account_1_friends')
Element.hide('account_1_contacts')
Element.hide('account_2_info')
Element.hide('account_2_friends')
Element.hide('account_2_contacts')
etc...
Is it possible to hide all blocks by ID mask account_*
(or by regexp)?
Use the double-dollar in prototypejs and CSS-selectors:
$$('*[id^="account_"]')
From version 1.7 prototypejs uses sizzle, the same selector-enginge as jquery uses, so you can use the jquery-documentation to learn about selectors. http://api.jquery.com/category/selectors/
If you are able to modify the html you can make life easier by using classes in your markup.
Instead of (for example):
<div id="account_1_info"></div>
<div id="account_1_friends"></div>
<div id="account_1_contacts"></div>
<div id="account_2_info"></div>
<div id="account_2_friends"></div>
<div id="account_2_contacts"></div>
Wrap the info blocks in a container
<div class="account" id="account_1">
<div class="info"></div>
<div class="friends"></div>
<div class="contacts"></div>
</div>
<div class="account" id="account_2">
<div class="info"></div>
<div class="friends"></div>
<div class="contacts"></div>
</div>
You can then target accounts, info blocks within accounts, or info blocks for all accounts.
You can use the attribute starts with selector in jQuery
$('[id^="account_"]').hide();
If you haven't already included jQuery in your site, it's never too late :)
- jQuery documentation
- jQuery selectors
Using jQuery with prototype may have some issues, read about it here.
I don't think it is possible in prototype. Element.hide uses $ method to find the element if passed the string and $ gives you element having id exact matched with given string.
if you want to reduce the line of code you can keep all ids in array and can hide them as
['id1', 'id2', 'id3'].each(Element.hide);
精彩评论