由于div在anchor里面,隐藏anchor也会隐藏div。您需要将 div 放在锚点旁边才能使其工作。使用next()方法选择div。
HTML
<a href="javascript:void(0);" id="viewdetail" style="color:green">view detail</a>
<div class="speakers dis-non" style="display: none">
test data to be shown
</div>
JS
$('#viewdetail').on('click', function(){
// show div with speakers class next to the anchor
$(this).next('div.speakers').show();
// hide the anchor
$(this).hide();
});
JSFiddle:http://jsfiddle.net/fw3sgc21/2/
编辑
如果您想将speakers div 包装在另一个 div 中,如下所示
<a href="javascript:void(0);" id="viewdetail" style="color:green">view detail</a>
<div class="12u">
<div class="speakers dis-non">
test data to be shown
</div>
</div>
使用以下 CSS
.dis-non
{
display: none;
}
这是应该显示 speakers div 并隐藏单击的锚点的 JS
$('#viewdetail').on('click', function(){
$(this).next().children('div.speakers').show();
$(this).hide();
});
JSFiddle:http://jsfiddle.net/fw3sgc21/6/
编辑 2
如果你想将锚点放在两个 div 中,如下所示
<div class="a">
<div class="b">
<a href="javascript:void(0);" id="viewdetail" style="color:green">view detail</a>
</div>
</div>
<div class="12u">
<div class="speakers dis-non">
test data to be shown
</div>
</div>
使用.parent() 方法两次选择<div class="a">,然后使用.next().children('div.speakers').show() 显示speakers div
$('#viewdetail').on('click', function(){
$(this).parent().parent().next().children('div.speakers').show();
$(this).hide();
});
JSFiddle:http://jsfiddle.net/fw3sgc21/8/