繁体   English   中英

单击链接时显示/隐藏Div

[英]Show/Hide Div when link is clicked

问候第一次张贴海报的长期读者...我看起来高低不一,却找不到我想要做的。

在我的网站上,将有100多个房间的“大型”建筑布局。 我希望能够单击房间,然后在页面的右侧拉出有关该房间的信息。 我希望该信息为“隐藏的Div”,谢谢大家。

<!DOCTYPE html>
<html>
<head>

<link rel="stylesheet" href="screen.css" type="text/css" media="all">

</head>
<body>
<div id="building">
<img src="http://upload.wikimedia.org/wikipedia/commons/9/9a/Sample_Floorplan.jpg"  alt="" usemap="#map">

<map id="map" name="map">
<area class="link" shape="rect" alt="" title="" coords="137,54,242,161" href="#one" target="" />
<area class="link" shape="rect" alt="" title="" coords="138,182,232,256" href="#two" target="" />
<area class="link" shape="rect" alt="" title="" coords="53,313,170,339" href="#three" target="" />
</map>
</div>
<div  id="menu">

<div class="tab" id="one">
This is One 

</div>
<div class="tab" id="two">
This is two 

</div>
<div class="tab" id="three">
This is three

</div>





</div>

</body>
</html>

这是我的CSS

  html { 
 padding:0px;
 margin:0px;
}

body {
background-color: #e1ddd9;
font-size: 12px;
font-family: Verdana, Arial, SunSans-Regular, Sans-Serif;
color:#564b47;  
padding:0px 20px;
margin:0px;
}

#building {
float: left;
width: 75%;
background-color: #fff;
margin:0px 0px 50px 0px;
overflow: auto;
} 
 #menu {
float: left;
width: 25%;
background-color: #ff99CC;
overflow: auto;

}

这是我的解决方案:

1)删除href="#one并将此HTML代码添加到area标签:

data-val="one" 

并将ID替换为您当时要显示的div的ID。

2)添加以下jQuery代码:

$(".link").click(function() {
    var which = $(this).data('val');
    $(".tab").hide();
    $('#'+which).show();
});

请在此JSFiddle中查看在当前代码上实现的代码。

  1. 将click事件绑定到链接类。 $('.link').on('click', function (e) { .... }
  2. 防止单击默认值e.preventDefault();
  3. 隐藏可见标签。 $('.tab').hide();
  4. 显示点击链接的详细信息。 $(INSERT SELECTOR HERE).show();

这是一个小提琴

这很简单:

div
{display: none;}

单击链接后,在jQuery中编写.show() 单击其他链接时,在jQuery中编写.hide()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM