Jac*_*ell 5 html javascript css function getelementsbyclassname
我写了一个脚本,它的目标是停止显示图像一和二,同时允许图像3保持显示并移动到它们的位置.当我使用div Id而不是div Classes时,它工作正常,但我更喜欢使用div类,所以我可以像这样对元素进行分组:
function myFunction() {
var y = document.getElementsByClassName("firstimage secondimage");
if (y.style.display === 'none') {
y.style.display = 'block';
} else {
y.style.display = 'none';
}
}
Run Code Online (Sandbox Code Playgroud)
而不是这个(为了节省空间,我应该选择包含更多元素):
function myFunction() {
var x = document.getElementById("firstimage");
if (x.style.display === 'none') {
x.style.display = 'block';
} else {
x.style.display = 'none';
}
var y = document.getElementById("secondimage");
if (y.style.display === 'none') {
y.style.display = 'block';
} else {
y.style.display = 'none';
}
}
Run Code Online (Sandbox Code Playgroud)
我认为只是将div id改为div类,#imagenumber改为.imagenumber(除了我上面描述的javascript的改变之外)会有效,但是当我这样做时脚本停止工作.我需要脚本以与我在下面粘贴的代码相同的方式运行,但是使用div类而不是div Id.请告诉我哪里出错了.
CSS:
#firstimage {
width: 100px;
height: 100px;
padding: 0px 0;
text-align: center;
background-color: green;
margin-top:20px;
color: white;
}
#secondimage {
width: 100px;
height: 100px;
padding: 0px 0;
text-align: center;
background-color: blue;
margin-top:20px;
color: white;
}
#thirdimage {
width: 100px;
height: 100px;
padding: 0px 0;
text-align: center;
background-color: red;
margin-top:20px;
color: white;
}
Run Code Online (Sandbox Code Playgroud)
HTML:
<button onclick="myFunction()">Try me</button>
<div id="firstimage">
DIV element.
</div>
<div id="secondimage">
A second DIV element.
</div>
<div id="thirdimage">
A third DIV element.
</div>
Run Code Online (Sandbox Code Playgroud)
使用Javascript:
function myFunction() {
var x = document.getElementById("firstimage");
if (x.style.display === 'none') {
x.style.display = 'block';
} else {
x.style.display = 'none';
}
var y = document.getElementById("secondimage");
if (y.style.display === 'none') {
y.style.display = 'block';
} else {
y.style.display = 'none';
}
}
Run Code Online (Sandbox Code Playgroud)
小智 8
document.getElementsByClassName 返回一个元素数组,因此您需要遍历该数组并对该循环中的每个元素进行操作.
您应该使用getElementsByClassName()或querySelectorAll()收集所有div.Klass(Klass 是任意名称)。以下代码片段使用querySelectorAll()详细信息在源代码中进行了注释。
function toggleDiv() {
// Collect all .image into a NodeList
var xs = document.querySelectorAll(".image");
// Declare i and qty for "for" loop
var i, qty = xs.length;
// Use "for" loop to iterate through NodeList
for (i = 0; i < qty; i++) {
// If this div.image at index [i] is "none"...
if (xs[i].style.display === 'none') {
// then make it "block"...
xs[i].style.display = 'block';
} else {
// otherwise set display to "none"
xs[i].style.display = 'none';
}
}
}Run Code Online (Sandbox Code Playgroud)
#firstimage {
width: 100px;
height: 100px;
padding: 0px 0;
text-align: center;
background-color: green;
margin-top: 20px;
color: white;
}
#secondimage {
width: 100px;
height: 100px;
padding: 0px 0;
text-align: center;
background-color: blue;
margin-top: 20px;
color: white;
}
#thirdimage {
width: 100px;
height: 100px;
padding: 0px 0;
text-align: center;
background-color: red;
margin-top: 20px;
color: white;
}Run Code Online (Sandbox Code Playgroud)
<button onclick="toggleDiv()">Try me</button>
<div id="firstimage" class='image'>
DIV element.
</div>
<div id="secondimage" class='image'>
A second DIV element.
</div>
<div id="thirdimage" class='img'>
A third DIV element.
</div>Run Code Online (Sandbox Code Playgroud)
在此函数中,仅使用“类似数组”的对象,例如上面代码片段中演示的 NodeList。数组的使用方式与代码片段中的使用方式相同。如果您想对 div 进行更高级的处理,例如在每个 div 上运行一个函数并返回,那么需要将“类似数组”的对象转换为数组才能运行map、forEach、slice等方法。