最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - How to get sibling element by its class name and then hide it using Javascript only? - Stack Overflow

programmeradmin3浏览0评论

I have this html structure:

  <div id="xyz"> </div>
  <div class="content"> </div>

i want to hide the element with class named content given the sibling element id which is xyz , in jQuery i can easily do it like this:

$("#xyz").siblings('.content').css({"dispaly": "none"});

how can i achieve the same thing using pure Javascript only ?

I have this html structure:

  <div id="xyz"> </div>
  <div class="content"> </div>

i want to hide the element with class named content given the sibling element id which is xyz , in jQuery i can easily do it like this:

$("#xyz").siblings('.content').css({"dispaly": "none"});

how can i achieve the same thing using pure Javascript only ?

Share Improve this question asked Jul 1, 2021 at 4:19 PadawanPadawan 1131 silver badge8 bronze badges 1
  • Does this help gomakethings./…? – Tushar Shahi Commented Jul 1, 2021 at 4:27
Add a ment  | 

4 Answers 4

Reset to default 4
document.querySelector("#xyz + .content").style.display = "none";

jsfiddle

document.getElementById("xyz").nextElementSibling.style.display = 'none';

try like this:

var x = document.getElementById("xyz").nextSibling;
while(true){   
   if(typeof(x) === "undefined"){
      break;
   }else if( x.className === "content"){
      x.style.display = 'none';
   }

   x = x.nextSibling;
}

Sorry, this code i still not testing yet.

what about just

document.getElementsByClassName("content")

u can use it like

document.getElementsByClassName("content")[0].style.display = "none"
发布评论

评论列表(0)

  1. 暂无评论