admin管理员组文章数量:1326337
Ok, this is my HTML
<div id="plus" style="margin-left: 10px; margin-top: 303px;">
<div id="plus-back"> </div>
<div id="plus-ex"> X </div>
</div>
NOTE: the #plus
element's inline styles was declared previuously by another script
And this is my JS
document.getElementById("plus").onclick = showNav
document.getElementById("plus-ex").onclick = hideNav
function showNav(){
this.style.width="200px"
this.style.height="200px"
document.getElementById("plus-ex").style.display="block"
}
function hideNav(){
document.getElementById("plus").style.width="48px"
document.getElementById("plus").style.height="48px"
}
Well.. this is what i have. The goal is simple, when you click #plus
, this is expanded to show some content, and appears a "X" that is inside #plus-ex
, and when you click that "X", #plus
go back to the start (that is a div with 48px of height and width thanks to stylesheet). The problem with this, is that hideNav()
is not doing a good work. When you click #plus
, showNav()
function is fired successfully, but after that, when you click the "X" and hideNav() is fired (successfully too), that should apply the initial style, but does anything. I have tested applying another CSS propieties like background-color
and works OK, but not with width
and height
.
I think that the problem is that i can't override the styles applied by showNav()
What should i do?
Ok, this is my HTML
<div id="plus" style="margin-left: 10px; margin-top: 303px;">
<div id="plus-back"> </div>
<div id="plus-ex"> X </div>
</div>
NOTE: the #plus
element's inline styles was declared previuously by another script
And this is my JS
document.getElementById("plus").onclick = showNav
document.getElementById("plus-ex").onclick = hideNav
function showNav(){
this.style.width="200px"
this.style.height="200px"
document.getElementById("plus-ex").style.display="block"
}
function hideNav(){
document.getElementById("plus").style.width="48px"
document.getElementById("plus").style.height="48px"
}
Well.. this is what i have. The goal is simple, when you click #plus
, this is expanded to show some content, and appears a "X" that is inside #plus-ex
, and when you click that "X", #plus
go back to the start (that is a div with 48px of height and width thanks to stylesheet). The problem with this, is that hideNav()
is not doing a good work. When you click #plus
, showNav()
function is fired successfully, but after that, when you click the "X" and hideNav() is fired (successfully too), that should apply the initial style, but does anything. I have tested applying another CSS propieties like background-color
and works OK, but not with width
and height
.
I think that the problem is that i can't override the styles applied by showNav()
What should i do?
Share Improve this question asked Aug 18, 2013 at 4:40 yukattayukatta 5252 gold badges7 silver badges16 bronze badges 3-
maybe the problem is
.style.display="block"
? – muratgu Commented Aug 18, 2013 at 4:45 -
@muratgu No, i deleted
.style.display="block"
but the problem persist. – yukatta Commented Aug 18, 2013 at 4:50 -
@Yavierre:
<div>
by default will shrink/wrap its height based on its content. I'm afraid you cannot set its height explicitly. – Khanh TO Commented Aug 18, 2013 at 4:56
1 Answer
Reset to default 4The problem is when you click X
the event is bubbling up to the #plus
div. You can prevent this by calling:
event.stopPropagation();
Update your code as follows and give it a try:
document.getElementById("plus").onclick = showNav
document.getElementById("plus-ex").onclick = hideNav
function showNav(){
this.style.width="200px"
this.style.height="200px"
document.getElementById("plus-ex").style.display="block" // Don't need this line as it's a block element i.e. already a div
event.stopPropagation(); // add this line
}
function hideNav(){
document.getElementById("plus").style.width="48px"
document.getElementById("plus").style.height="48px"
event.stopPropagation(); // add this line
}
本文标签: cssJavascriptOverriding styles previously declared in another functionStack Overflow
版权声明:本文标题:css - Javascript - Overriding styles previously declared in another function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742200057a2431747.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论