简体   繁体   English

使用javascript隐藏div

[英]hide divs using javascript

I would like to hide divs that have no ids associated with them using javascript. 我想使用javascript隐藏没有与它相关联的id的div。 For example, in sharepoint .ms-globalbreadcrumb doesn't have an id. 例如,在sharepoint .ms-globalbreadcrumb中没有id。

frame = document.getElementById('frame1'); 
frame.contentWindow.document.getElementById('ctl00_PlaceHolderGlobalNavigation_PlaceHolderGlobalNavigationSiteMap_GlobalNavigationSiteMap').style.display='none'; 

The above code works for pieces that have ids but I am not sure how to achieve this for other divs with no ids. 上面的代码适用于有id的部分,但我不知道如何为没有id的其他div实现这个。

Thanks, 谢谢,

You would make your life a lot easier using something that normalizes access to the DOM, so that the same code (for everything - forms, events, object properties, etc etc) works across all browsers. 使用规范化访问DOM的东西可以让你的生活更轻松,因此相同的代码(对于所有东西 - 表单,事件,对象属性等)适用于所有浏览器。 Using JQuery it's just: 使用JQuery它只是:

$('div').hide();

to hide all divs...and there are a huge range of 'selectors' to refine your selection. 隐藏所有div ...并且有大量的“选择器”来优化您的选择。

Case 1 情况1
If you want to hide all divs with no id then you would have to loop all divs and hide them based on that criteria. 如果你想隐藏所有没有id的div,那么你必须循环所有的div并根据这个标准隐藏它们。 ( find the divs with the .getElementsByTagName() ) 找到带有.getElementsByTagName()的div

var alldivs = document.getElementsByTagName('div');
for( var i = 0; i < alldivs.length; i++) {    
       alldivs[i].style.display = "none";
    }

Case 2 案例2
If you want to find elements based on a class, like in your example the .ms-globalbreadcrumb then ( find the elements with the class with the .getElementsByClassName() ) 如果你想找到一个基于类的元素,比如你的例子中的.ms-globalbreadcrumb那么( 找到带有.getElementsByClassName()的类的元素

var allbyclass = document.getElementsByClassName('ms-globalbreadcrumb');
for( var i = 0; i < allbyclass.length; i++) {    
       allbyclass[i].style.display = "none";
    }

( the getElementsByClassName will not work for pre IE9 versions of IE ) getElementsByClassName不适用于IE的IE9之前的版本

example with both cases at http://jsfiddle.net/gaby/H3nNr/ 两个案例的例子都在http://jsfiddle.net/gaby/H3nNr/


suggestion 建议

Use jQuery which allows for a wide variety of selectors and complex traversing of the DOM to find what you want.. 使用jQuery ,它允许各种各样的选择器和复杂的遍历 DOM来找到你想要的东西。

  • Case 1 in jQuery would be $('div:not([id])').hide(); jQuery中的case 1将是$('div:not([id])').hide();
  • Case 2 in jQuery would be $('.ms-globalbreadcrumb').hide(); jQuery中$('.ms-globalbreadcrumb').hide(); 2将是$('.ms-globalbreadcrumb').hide();

If you can locate the divs in the DOM, then you should be able to hide them. 如果你可以在DOM中找到div,那么你应该能够隐藏它们。 For example, if a parent / grandparent etc div has an ID, you can go down the DOM tree using Javascript until you have found the element you want to hide. 例如,如果父/祖父母等div具有ID,则可以使用Javascript在DOM树中查找,直到找到要隐藏的元素。

var elem = document.getElementById("topelement");
var elem2 = elem.firstChild.firstChild;
elem2.style.display = "none";

JQuery has lots of selectors for making this kind of thing easy. JQuery有很多选择器可以让这种事情变得简单。

so theres other methods you can use you can get them by tagname or classes 所以您可以使用的其他方法可以通过标记名或类来获取它们

 document.getElementsByClassName
 document.getElementsByTagName

they return list of elements you need to iterate threw 它们返回你需要迭代扔掉的元素列表

so your example shud be 所以你的榜样很糟糕

var ellements = frame.contentWindow.document
   .getElementsByClassName("ms-globalbreadcrumb");
for(i in ellements) {
  ellements[i].style.display = "none";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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