开发者

Changing display property dynamically

I want to change the style:display property (none or block) of my div (displaydiv) according to the value o开发者_JS百科f a global variable (disp). I want to check the value of this variable on page load and set the styel: display according to the value disp.

i set the value of disp as "none" in javascript.

i want to change the value within HTML TAG

But this div is always visible. Please help me


<script>
function hidemydiv() {
 if(disp == 'none') document.getElementById('displaydiv').style.display = 'none';
}
</script>

<body onload="hidemydiv()">
<div id="displaydiv">
    Lorem ipsum dolor sith amet
</div>
</body>

I just wonder what kind of global variable disp is. Is it in javascript? PHP? Where/when/how do you set it?


We can't tell without code but it sounds very much like you're executing your change-style JS inline before the target DIV has loaded.

Does your browser report errors on the page?

Is your JS code bound to the onload event somehow?

Is the style actually applied but overidden (check with firebug)?


Attach a function to the window onload event.

window.onload = function () {
    var elem = document.getElementById(divId);

    if (disp === "none") {
        elem.style.display = "none";
    }
    else {
        elem.style.display = "block";
    }
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜