javascript 如何在javascript中为div设置可见属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18379894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 11:41:43  来源:igfitidea点击:

how to set visible property for div in javascript

javascriptasp.net

提问by user96888

I cannot able to access control in the below way

我无法通过以下方式访问控制

document.getElementById("_ctl0_ContentPlaceHolder1_divDocSearch").style.visibility = 'visible';

but i can able to access as

但我可以访问

var div = document.getElementById("_ctl0_ContentPlaceHolder1_divDocSearch");
div.style.visibility = 'visible';

How to access with above line, is there any toggle property available for this ?

如何访问上面的行,是否有可用的切换属性?

采纳答案by Sunil Mishra

You can create a function like below

您可以创建一个如下所示的函数

function toggle(obj){
    if(obj.style.visibility == "visible")
        obj.style.visibility = 'hidden';
    else
        obj.style.visibility = 'visible';
}

And then call them on the each element using toggle(object);

然后使用toggle(object);在每个元素上调用它们

回答by Jason Evans

Try

尝试

div.style = 'display:none';

div.style = 'display:none';

to hide the div. and thus

隐藏div。因此

div.style = 'display:inline';// or block, or whatever you need.

div.style = 'display:inline';// 或块,或任何你需要的。

to display the div.

显示div。

EDIT:

编辑:

Just foudn this: http://www.w3schools.com/css/css_display_visibility.asp

刚刚发现这个:http: //www.w3schools.com/css/css_display_visibility.asp

so it looks like you can do

所以看起来你可以做到

div.visibility = 'hidden';

div.visibility = 'hidden';

回答by rakeshjain

You can call the below function on click of all those divs passing in the id of the particular div being clicked. It finds all divs by using document.getElementsByTagName and hides them. Then it again makes visible the div whose id is passed

您可以在单击所有传递被单击的特定 div 的 id 的 div 时调用以下函数。它使用 document.getElementsByTagName 查找所有 div 并隐藏它们。然后它再次使传递了 id 的 div 可见

function showdiv(divid) {
  divs = document.getElementsByTagName("div");
  count=divs.length;
  for(i=0;i<count;i++) {
    divs[i].style.visibility="hidden";
  }
  document.getElementById(divid).style.visibility="visible";
}

回答by sangram parmar

Try this:

试试这个:

document.getElementById("<%= divDocSearch.ClientID %>").style.display = 'none';