javascript - Hidden div flashing on page load - Stack Overflow

In my Rails app, I'm trying to hide a div (box) on page load in the Javascript function below.Th

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.

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.

Share Improve this question asked Jun 26, 2009 at 3:32 Sol IrvineSol Irvine
Add a ment  | 

1 Answer 1

Reset to default 8

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?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745185258a4615605.html

相关推荐

  • javascript - Hidden div flashing on page load - Stack Overflow

    In my Rails app, I'm trying to hide a div (box) on page load in the Javascript function below.Th

    9小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信