gpt4 book ai didi

javascript - 在创建过滤器的 JavaScript 代码中输入错误

转载 作者:行者123 更新时间:2023-12-03 01:13:37 25 4
gpt4 key购买 nike

我改变了我的方法并制作了一个带有按钮的选择器,所以我这样做了:

此代码的目的是通过单击显示与按钮对应的图像,知道有多个具有不同名称的图像HTML 代码:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>repl.it</title>
<link href="index.css" rel="stylesheet" type="text/css"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
</head>
<body>
<script src="index.js"></script>
<div id="btnconteneur">
<button class="btn active" onclick="filter('all')">Show all</button>
<button class="btn" onclick="filter('AttributionCodeConfidentiel')">Attribution Code confidentiel</button>
<button class="btn" onclick="filter('bis')">Bis</button>
</div>
<div class="conteneur">
<div class=" filtre AttributionCodeConfidentiel"><img src="test/AttributionCodeConfidentiel.png" alt="" /></div>
<div class=" filtre AttributionCodeConfidentiel"><img src="test/AttributionCodeConfidentiel_201706.png" alt="201706" /></div>
<div class=" filtre AttributionCodeConfidentiel"><img src="test/AttributionCodeConfidentiel_201707.png" alt="201707" /></div>
<div class=" filtre AttributionCodeConfidentiel"><img src="test/AttributionCodeConfidentiel_201708.png" alt="201708" /></div>
<div class=" filtre AttributionCodeConfidentiel"><img src="test/AttributionCodeConfidentiel_201709.png" alt="201709" /></div>
<div class=" filtre AttributionCodeConfidentiel"><img src="test/AttributionCodeConfidentiel_201710.png" alt="201710" /></div>
<div class=" filtre AttributionCodeConfidentiel"><img src="test/AttributionCodeConfidentiel_201711.png" alt="201711" /></div>
<div class=" filtre AttributionCodeConfidentiel"><img src="test/AttributionCodeConfidentiel_201712.png" alt="201712" /></div>
<div class=" filtre AttributionCodeConfidentiel"><img src="test/AttributionCodeConfidentiel_201801.png" alt="201801" /></div>
<div class=" filtre AttributionCodeConfidentiel"><img src="test/AttributionCodeConfidentiel_201802.png" alt="201802" /></div>
<div class=" filtre AttributionCodeConfidentiel"><img src="test/AttributionCodeConfidentiel_201803.png" alt="201803" /></div>
<div class=" filtre AttributionCodeConfidentiel"><img src="test/AttributionCodeConfidentiel_201804.png" alt="201804" /></div>
<div class=" filtre AttributionCodeConfidentiel"><img src="test/AttributionCodeConfidentiel_201805.png" alt="201805" /></div>
<div class=" filtre AttributionCodeConfidentiel"><img src="test/AttributionCodeConfidentiel_201806.png" alt="201806" /></div>
<div class=" filtre AttributionCodeConfidentiel"><img src="test/AttributionCodeConfidentiel_201807.png" alt="201807" /></div>
<div class=" filtre AttributionCodeConfidentiel"><img src="test/AttributionCodeConfidentiel_201808.png" alt="201808" /></div>
<div class=" filtre bis"><img src="test/bis.png" alt="" /></div>
<div class=" filtre bis"><img src="test/bis_201706.png" alt="201706" /></div>
<div class=" filtre bis"><img src="test/bis_201707.png" alt="201707" /></div>
<div class=" filtre bis"><img src="test/bis_201708.png" alt="201708" /></div>
<div class=" filtre bis"><img src="test/bis_201709.png" alt="201709" /></div>
<div class=" filtre bis"><img src="test/bis_201710.png" alt="201710" /></div>
<div class=" filtre bis"><img src="test/bis_201711.png" alt="201711" /></div>
<div class=" filtre bis"><img src="test/bis_201801.png" alt="201801" /></div>
<div class=" filtre bis"><img src="test/bis_201802.png" alt="201802" /></div>
<div class=" filtre bis"><img src="test/bis_201803.png" alt="201803" /></div>
<div class=" filtre bis"><img src="test/bis_201804.png" alt="201804" /></div>
<div class=" filtre bis"><img src="test/bis_201805.png" alt="201805" /></div>
<div class=" filtre bis"><img src="test/bis_201806.png" alt="201806" /></div>
<div class=" filtre bis"><img src="test/bis_201807.png" alt="201807" /></div>
<div class=" filtre bis"><img src="test/bis_201808.png" alt="201808" /></div>
</body>
</html>

和我的 JavaScript :

function filter(c) {
var x, i;
x = document.getElementsByClassName("filtre");
if (c == "all") c = "";
// Add the "show" class (display:block) to the filtered elements, and remove the "show" class from the elements that are not selected
for (i = 0; i < x.length; i++) {
w3RemoveClass(x[i], "show");
if (x[i].className.indexOf(c) > -1) w3AddClass(x[i], "show");
}
}

// Show filtered elements
function w3AddClass(element, name) {
var i, arr1, arr2;
arr1 = element.className.split(" ");
arr2 = name.split(" ");
for (i = 0; i < arr2.length; i++) {
if (arr1.indexOf(arr2[i]) == -1) {
element.className += " " + arr2[i];
}
}
}

// Hide elements that are not selected
function w3RemoveClass(element, name) {
var i, arr1, arr2;
arr1 = element.className.split(" ");
arr2 = name.split(" ");
for (i = 0; i < arr2.length; i++) {
while (arr1.indexOf(arr2[i]) > -1) {
arr1.splice(arr1.indexOf(arr2[i]), 1);
}
}
element.className = arr1.join(" ");
}

// Add active class to the current control button (highlight it)
var btnContainer = document.getElementById('btnconteneur');
var btns = btnContainer.getElementsByClassName("btn");
for (var i = 0; i < btns.length; i++) {
btns[i].addEventListener("click", function() {
var current = document.getElementsByClassName("active");
current[0].className = current[0].className.replace(" active", "");
this.className += " active";
});
}

一切看起来都很好,但是当我运行时出现错误:

TypeError: document.getElementById(...) is null

我不明白为什么会出现这个错误:(

最佳答案

将脚本 index.js 包含在 body 标记的开头后,它就会被执行,并且当它运行在 var btnContainer = document.js 时。 getElementById('btnconteneur');,它没有找到元素并返回null

因此,您应该在 body 标记末尾之前包含 index.js,以便所有元素在执行脚本时都可用。

<script src="index.js"></script>
</body>

关于javascript - 在创建过滤器的 JavaScript 代码中输入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52115296/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com