【发布时间】:2018-06-09 06:05:41
【问题描述】:
我想让一个 div 的内容在单击另一个 div 时显示出来。但我已经试过了:
function loadFishContent() {
if (div is clicked) {
document.getElementById('fish').style.display = "block";
document.getElementById('dogs').style.display = "none";
document.getElementById('cats').style.display = "none";
}
};
我试过了:
if (document.getElementByID('menu-fish') == true) {
//do action
}
但该操作适用于我点击的所有 div。
我的 html 是:
<div id="menu-fish" onclick="loadFishContent()"> Fish </div>
<div id="menu-dogs" onclick="loadDogsContent()"> Dogs </div>
<div id="menu-cats" onclick="loadCatsContent()"> Cats </div>
<div id="content">
<div id="fish">
<h2> Fish </h2>
<img src="fish.jpg"/>
<p> Information about fish in the store goes here.</p>
</div>
<div id="dogs">
<h2> Dogs </h2>
<img src="dog.jpg" />
<p> Information about dogs in the store go here.</p>
</div>
<div id="cats">
<h2> Cats </h2>
<img src="cat.jpg" />
<p> Information about cats in the store go here.</p>
</div>
</div>
问题是,我如何定位在 if 条件下被点击的 div?
【问题讨论】:
-
感谢您的评论。但他们只写“目标”。我如何实际定位被点击的 div?
标签: javascript html function onclick