简体   繁体   中英

Hidden div flashing on page load

In my Rails app, I'm trying to hide a div (box) on page load in the Javascript function below. This function loops through a series of checkboxes with the same name (cb). If any of the checkboxes are checked, it should show the div.

function BoxCheck(cb,box){
var cbs=document.getElementsByName(cb);
var d=document.getElementById(box);
d.style.display = 'none'; 
var flag_check=0

for (var zxc0=0;zxc0<cbs.length;zxc0++){
  if (cbs[zxc0].checked){
    flag_check=flag_check+1
    } else
    { }
}
if (flag_check > 0){
    d.style.display = 'block'; 
    document.getElementById('multi_control_spacer').style.display = 'block';      
 } else {
    d.style.display = 'none'; 
    document.getElementById('multi_control_spacer').style.display = 'none'; 
 }
}

The function fires on load with:

<body onload="javascript:BoxCheck('doc_ids[]','multi_control');">

The problem is that when no checkboxes are selected, the div flashes on momentarily, and then disappears.

Here's the CSS:

#multi_control {
    padding: 10px;
    background: #333;
}

I tried setting the css to display:none, but then I can't seem to make it switch to back to display:block with Javascript.

Why not? Have you tried:

element.style.display = 'block';

How about putting style="display:none" into the div tag so it's hidden to begin with?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM