简体   繁体   中英

Change CSS Element with JS,JS?

我现在在一个Question / Anwser脚本上有大约40个问题,我想隐藏所有其他消息,然后单击具有ID的消息,但窗口重叠,那么我该如何隐藏所有其他ID并只显示一个Javascript?

The following examples will vary depending on your HTML structure. They are meant as examples, not solutions for your specific situation.

Changing the style of a single element can be done rather quickly:

document.getElementById("foo").style.display = "none";

You could hide all others like this:

var myDivs = document.getElementsByTagName("DIV");
for (var i = 0; i < myDivs.length; i++) {
  if (myDivs[i].id != "foo") {
    myDivs[i].style.display = "none";
  } else {
    myDivs[i].style.display = "block";
  }
}

If you're open to using a framework, like jQuery , you could do the same thing even quicker:

$(".question .title").click(function(){
 $(this)
   .next().slideDown()
   .closest(".question").siblings()
   .find(".body").slideUp(); 
});​

Online Demo of jQuery: http://jsbin.com/opisa/edit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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