formatting lose when showing a previously hidden div

半城伤御伤魂 提交于 2019-12-25 06:59:55

问题


So my webpage shows a new div when you click on an existing div. This hidden div has exactly the same formatting as the existing visible div, but when it is made to appear, all of that formatting is lost and I can't quite work out why. Here's the code:

<div id="visible" class="visibleDiv" onclick="expandItem()">
   Stuff here
</div>

<div id="invisible" class="hiddenDiv">
    Stuff here
</div>

And here's my JavaScript:

function expandItem() {

if (document.getElementById("invisible").style.display == '') {
    document.getElementById("invisible").style.display = 'block';

}

Any help is greatly appreciated!


回答1:


Try This

<div id="visible" class="visibleDiv" onclick="expandItem()">
    Stuff here
</div>
<div id="invisible" style="display:none;" class="hiddenDiv">
    Stuff here
</div>

And make change in javascript

function expandItem() {
if (document.getElementById("invisible").style.display == 'none') {
    document.getElementById("invisible").style.display = 'block';

}



回答2:


yeah this should fetch the solution

function expandItem() {
if (document.getElementById("invisible").style.display == 'none' || document.getElementById("invisible").style.display == '') {
    document.getElementById("invisible").style.display = 'block';

}

this line

document.getElementById("invisible").style.display == ''

is irrelavent , the main code to be executed is

document.getElementById("invisible").style.display == 'none'


来源:https://stackoverflow.com/questions/13191860/formatting-lose-when-showing-a-previously-hidden-div

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!