admin管理员组文章数量:1318994
I seem to have same issue as the one I posted before. I want to hide all the divs that are there by default and only display one. Then the user can click on a side tab to display another. Problem is the divs are only hidden for a second after page loads but reappear soon after. This is code that's suppose to hide the divs of the page and only display the div with id of "pple":
$("a#link2").click(function(){$("#content > div").hide(); $("#pple").show();});
markup:
home.html:
<li><a href="about.html" id="link2">About</a></li>
about.html:
<div id="content">
<div class="tabContent" id="pple">
<p>
Content
<p>
</div>
<div class="tabContent" id="serv">
<p>
Content
</p>
</div>
<div class="tabContent" id="sol">
<p>
Content
</p>
</div>
</div>
Thanks for any response.
I seem to have same issue as the one I posted before. I want to hide all the divs that are there by default and only display one. Then the user can click on a side tab to display another. Problem is the divs are only hidden for a second after page loads but reappear soon after. This is code that's suppose to hide the divs of the page and only display the div with id of "pple":
$("a#link2").click(function(){$("#content > div").hide(); $("#pple").show();});
markup:
home.html:
<li><a href="about.html" id="link2">About</a></li>
about.html:
<div id="content">
<div class="tabContent" id="pple">
<p>
Content
<p>
</div>
<div class="tabContent" id="serv">
<p>
Content
</p>
</div>
<div class="tabContent" id="sol">
<p>
Content
</p>
</div>
</div>
Thanks for any response.
Share Improve this question edited Jun 24, 2010 at 11:46 JohnMerlino asked Jun 24, 2010 at 1:31 JohnMerlinoJohnMerlino 3,9284 gold badges61 silver badges91 bronze badges 2-
Is this page
about.html
? It looks like your code is reloading the page, unless you're preventing the anchor from being followed in additional code you haven't posted. – Nick Craver Commented Jun 24, 2010 at 1:35 - no the link is not on about.html. The link belongs to home.html. – JohnMerlino Commented Jun 24, 2010 at 11:45
2 Answers
Reset to default 9You need to return false;
on your handler for the <a>
element.
This is because the default behavior of the <a>
element is occurring and reloading the page, so you need to disable that behavior.
$("a#link2").click(function(){
$("#content > div").hide();
$("#pple").show();
return false; // Will prevent default behavior
// but it also prevents event bubbling
});
Alternatively, you could call event.preventDefault()
$("a#link2").click(function( event ){
event.preventDefault(); // Prevents the default behavior
// and event bubbling is still intact
$("#content > div").hide();
$("#pple").show();
});
http://api.jquery./event.preventdefault/
Try to prevent default after the divs are hide
so you can do
...function(event)
event.preventDefault()...
本文标签:
版权声明:本文标题:javascript - jquery hide method only hides div for a second but then div reappears automatically - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742054678a2418221.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论