我想知道是否有更好/更清洁的方法来完成我在下面的代码中所拥有的内容。
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的新手。
答案 0 :(得分:8)
将所有必需元素设为一个类,然后通过getElementsByClassName(..)
选择它们。
(并且可能使用jQuery以更少的痛苦来做同样的事情)
答案 1 :(得分:4)
您可以使用循环:
var elements = ['jobDescription', 'updateButton', 'equipmentList', 'jobDesc', 'equipRan'];
for(i = 0; i < elements.length; i++) {
document.getElementById(elements[i]).style.display = "block";
}
答案 2 :(得分: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";
或者,将所有这些元素放在一个容器中,然后切换它。
答案 3 :(得分:0)
这不是可以在一行中完成的事情。但是,使用jQuery,可以为元素提供一个类,并使用一个漂亮的jQuery方法来操作它的样式。但是对于纯JS,你可以使用一个字符串数组(ids),迭代它并用这些id设置元素的样式。
[ 'jobDescription', 'updateButton', 'equipmentList',
'jobDesc', 'equipRan' ].forEach(function( iden ) {
document.getElementById( iden ).style.display = "block";
});
答案 4 :(得分: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>