admin管理员组文章数量:1429983
Im using the following JS
<a href=# onClick="if($(this).next('div').css('display') == 'none') { $(this).next('div').show('fast'); } else { $(this).next('div').hide('fast'); } return false;">Add a Street Address</a>
and the following html
<div id=entry>
<div id=label>Street</div>
<div id=input><input name="" type="text" class="longtext" /></div></div>
<div id=entry>
<div id=label>City/Town</div>
<div id=input><input name="" type="text" class="longtext" /></div></div>
How would i modify the JS to show/hide both of those divs at once, and then make the link disappear?
Im using the following JS
<a href=# onClick="if($(this).next('div').css('display') == 'none') { $(this).next('div').show('fast'); } else { $(this).next('div').hide('fast'); } return false;">Add a Street Address</a>
and the following html
<div id=entry>
<div id=label>Street</div>
<div id=input><input name="" type="text" class="longtext" /></div></div>
<div id=entry>
<div id=label>City/Town</div>
<div id=input><input name="" type="text" class="longtext" /></div></div>
How would i modify the JS to show/hide both of those divs at once, and then make the link disappear?
Share Improve this question asked Nov 1, 2009 at 11:01 mrpatgmrpatg 10.1k44 gold badges114 silver badges170 bronze badges3 Answers
Reset to default 2a. you better off using class instead of id where both share the same value (e.g. entry)
<div class=entry>
<div id=label>Street</div>
<div id=input><input name="" type="text" class="longtext" /></div></div>
<div class=entry>
<div id=label>City/Town</div>
<div id=input><input name="" type="text" class="longtext" /></div></div>
b. Hiding both divs can be done as:
$('.entry').hide();
hiding the clicked link
$(this).hide();
return true;
e.g.
<a id='myLinkId' href='#'>Click To Hide</a>
$('a#myLinkId').click(function(){
$('.entry').hide();
$(this).hide();
return true;
});
First give an id to your link tag like 'link', then give you two dives two different ids, then write a js function like this :
show_hide = function()
{
if(document.getElementById('link').style.display == 'none'){
document.getElementById('link').style.display = 'inline';
document.getElementById('entry1').style.display = 'inline';
document.getElementById('entry2').style.display = 'inline';
}else{
document.getElementById('link').style.display = 'none';
document.getElementById('entry1').style.display = 'none';
document.getElementById('entry2').style.display = 'none';
}
}
IDs need to be unique so I have changed the ids to classes. Also, I have changed the code from being inline to being unobtrusive The jQuery would be something like the following
$(function() {
$('#addressLink').click(function() {
$('div.entry').toggle();
$(this).hide();
return false; // prevent the default anchor behaviour
});
});
<a id="addressLink" href="#">Add a Street Address</a>
<div class="entry">
<div class="label">Street</div>
<div class="input"><input name="" type="text" class="longtext" /></div>
</div>
<div class="entry">
<div class="label">City/Town</div>
<div class="input"><input name="" type="text" class="longtext" /></div>
</div>
Assuming that you want the <div>
elements to be hidden initially, simply add $('div.entry').hide();
into the document ready handler. There are other techniques that you can use here, but I would suggest hiding using JavaScript for graceful degradation purposes
Here's a Working Demo. Add /edit to the URL to see the code
本文标签: javascriptjquery showhide two divs on click and hide linkStack Overflow
版权声明:本文标题:javascript - jquery showhide two divs on click and hide link - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745497211a2660861.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论