Hide div by class id

匿名 (未验证) 提交于 2019-12-03 08:42:37

问题:

If I have <div id="ad1" class="ad"> and <div id="ad2" class="ad"> how can I hide both by hiding all divs with class ad

I tried document.getElementsByClassName(ad).style.visibility="hidden"; but only this works
function hidestuff(boxid){ document.getElementById(boxid).style.visibility="hidden"; }

回答1:

As Matt Ball's clue left, you need to iterate through the results of your getElementsByClassName result.

Try something along the lines of:

    var divsToHide = document.getElementsByClassName("ad");      for(var i = 0; i < divsToHide.length; i++)     {     divsToHide[i].style.visibility="hidden";     } 


回答2:

$('.divClassName').hide(); 

This will solve your problem.

In your case it will be like below. $('.ad').hide(); This will hide all the elements with class name 'ad'.



回答3:

use jquery .hide()

jsfiddle demo

$('.ad').hide(); 


回答4:

To make the content visible which is inside iframe - pls try below:

var frame = document.getElementById("chatFeed");  var msg2 =frame.contentDocument.getElementsByClassName("publisherWrapper"); for (i = 0; i < msg2.length; i++) { msg2[i].style.visibility="visible"; } 


文章来源: Hide div by class id
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!