2012-12-31 38 views
3

我想知道是否有更好/更干净的方式来完成我在下面的代码中的内容。通过ID在一行中选择多个元素

var updateJob = function(){ 
    document.getElementById("jobDescription").style.display = "block"; 
    document.getElementById("updateButton").style.display = "block"; 
    document.getElementById("equipmentList").style.display = "block"; 
    document.getElementById("jobDesc").style.display = "block"; 
    document.getElementById("equipRan").style.display = "block"; 
} 

我想只有一个行会全部取消,如果可能我已经试过document.getElementById("jobDescription" + "updateButton" + etc...).style.display = "block";的元素,但它不工作。我是JavaScript新手。

+2

你应该认真考虑[jQuery的](http://jquery.com/)。 –

+0

如果你的目标是现代浏览器,你也可以使用['document.querySelectorAll'](https://developer.mozilla.org/en-US/docs/DOM/Document.querySelectorAll)(尽管你仍然需要循环浏览他们)。 –

+0

添加一个CSS类到元素,然后全部按类切换它们 – scunliffe

回答

8

给所有你需要的元素一个类,并通过getElementsByClassName(..)选择它们。

(也许使用jQuery做同样的事情用少得多的疼痛)

+0

+1,这个解决方案比一个循环更好:) –

+0

@HunterMcMillen你仍然需要循环getElementsByClassName,因为它返回一个节点列表 – 0x499602D2

+0

@David true,但是你消除了硬编码的字符串数组,这使得他的代码更加清晰。 –

4

你可以使用一个循环:

var elements = ['jobDescription', 'updateButton', 'equipmentList', 'jobDesc', 'equipRan']; 

for(i = 0; i < elements.length; i++) { 
    document.getElementById(elements[i]).style.display = "block"; 
} 
0

试试这个:

var toggle = ["jobDescription","updateButton","equipmentList","jobDesc","equipRan"], 
    l = toggle.length, i; 
for(i=0; i<l; i++) document.getElementById(toggle[i]).style.display = "block"; 

或者,把所有这些元素在一个容器中,只需拨动那个。

0

这是不是可以在一个线来完成。但是,使用jQuery可以给元素一个类并使用漂亮的jQuery方法操作它的样式。但是对于纯JS,你可以使用一个字符串数组(id),迭代它并用这些id设置元素的样式。

[ 'jobDescription', 'updateButton', 'equipmentList', 
    'jobDesc', 'equipRan' ].forEach(function(iden) { 

    document.getElementById(iden).style.display = "block"; 

}); 
0

试试这个,我相信它会在所有现代浏览器的工作:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>For Loop Element</title> 
</head> 
<body> 

    <div id="divOne"> 

     <p id="paraOne">It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> 
     <p id="paraTwo">Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p> 
     <p id="paraTre">It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> 

    </div> 

    <div id="divTwo">Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</div> 


    <script> 
      var allElem = document.querySelectorAll('#divOne, #paraOne, #paraTwo, #paraTre,#divTwo'); 
      console.log(allElem); 

      for(var i = 0; i < allElem.length; i += 1) 
      { 
       allElem[i].style.border= '1px solid #002D55'; 
      } 
    </script> 
</body> 
</html>