As soon as I click on 'Hide' link below, my page goes to the bottom. How can I make it go to the place from where it opened?
My code:
<li><a href="ja开发者_JAVA技巧vascript:ReverseDisplay('basic2')">How do I maximize battery life?</a></li>
<div id="basic2" style="display:none; background-color:#fff; padding:5px; margin-left: 11px;">
<p>Wadfasfsafasfas</p>
<a href="#" onclick="ReverseDisplay('basic2');">Hide</a></li>
<div id="basic2" style="display:none; background-color:#fff; padding:5px; margin-left: 11px;">
</div>
<p> </p>
<hr />
</div>
Function:
function ReverseDisplay(d) {
if(document.getElementById(d).style.display == "none")
{
document.getElementById(d).style.display = "block";
}
else
{
document.getElementById(d).style.display = "none";
}
}
Whenever you have an onClick in a link (like onclick="ReverseDisplay('basic2');"
you want to make sure it returns false
.
Something like this should work : onclick="ReverseDisplay('basic2');return false;"
You've got duplicate element ids "basic2"
Edit I think what you're looking for is something like this. Where "Hide" toggles the parent element:
<li><a href="javascript:ReverseDisplay('basic1')">How do I maximize battery life?</a></li>
<div id="basic1" style="display:none; background-color:#fff; padding:5px; margin-left: 11px;">
<p>Wadfasfsafasfas</p>
<a href="#" onclick="ReverseDisplay('basic1');">Hide</a></li>
<p> </p>
<hr />
</div>
<script>
function ReverseDisplay(d) {
if(document.getElementById(d).style.display == "none") { document.getElementById(d).style.display = "block"; }
else { document.getElementById(d).style.display = "none"; }
}
</script>
精彩评论