2009-11-01 159 views

回答

2

a。您最好使用class而不是id,其中两者共享相同的值(例如条目)

<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。隐藏的div都可以做到:

$('.entry').hide(); 

躲在被点击链接

$(this).hide(); 
return true; 

例如

<a id='myLinkId' href='#'>Click To Hide</a> 

$('a#myLinkId').click(function(){ 
    $('.entry').hide(); 
    $(this).hide(); 
    return true; 
}); 
+0

标签和输入div的id必须是唯一的,并且返回true将触发默认的锚定行为,这不是您想要的情况 – 2009-11-01 13:23:25

1

ID必须是唯一的,所以我已将ID更改为类。另外,我已经从在线到是不显眼的jQuery的改变了代码会像下面

$(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> 

假设你想要的<div>元素最初隐藏,只需添加$('div.entry').hide();到文档准备好处理程序。有迹象表明,你可以在这里使用其他技术,但我会建议使用JavaScript躲藏graceful degradation目的

这里有一个Working Demo。添加/编辑到URL查看代码

+0

$( 'addressLink')应该是$( ​​'#addressLink') – mahemoff 2009-11-01 11:12:39

+0

@mahemoff - 感谢 – 2009-11-01 11:19:56

2

先举一个ID像“链接”你的链接标签,然后给你两个跳两个不同的ID,然后写一个js函数是这样的:

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'; 
    } 
} 
+0

问题jQuery的下标记为好,为什么不使用jQuery的隐藏/显示方式供应? – 2009-11-01 11:20:41

+0

我不知道jQuery是什么;) – AliBZ 2009-11-01 11:23:31