我的JS元素过滤器为什么不起作用?

huangapple go评论72阅读模式
英文:

Why are my JS element filters not working?

问题

我想创建一个筛选器,点击每个框将删除不符合该描述的任何元素。我尝试添加一些在W3上找到的代码,但这对我不起作用:
https://www.w3schools.com/howto/howto_js_filter_elements.asp

当我点击框时,页面上没有过滤任何内容。

项目图片

<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->

// Filters
filterSelection("all");

function filterSelection(c) {
  var x, i;
  x = document.getElementsByClassName("filterDiv");
  if (c == "all") c = "";
  // 将“show”类(display:block)添加到筛选元素,并从未选中的元素中删除“show”类
  for (i = 0; i < x.length; i++) {
    w3RemoveClass(x[i], "show");
    if (x[i].className.indexOf(c) > -1) w3AddClass(x[i], "show");
  }
}

// 显示筛选元素
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];
    }
  }
}

// 隐藏未选中的元素
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(" ");
}

// 将活动类添加到当前控制按钮(突出显示它)
var btnContainer = document.getElementById("myBtnContainer");
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";
  });
}
<!-- language: lang-css -->

/*筛选器*/

.filter-container {
  overflow: hidden;
}

.filterDiv {
  float: left;
  background-color: #2196f3;
  color: #ffffff;
  width: 100px;
  line-height: 100px;
  text-align: center;
  margin: 2px;
  display: none; /* 默认情况下隐藏 */
}

/* 将“show”类添加到筛选元素 */

.show {
  display: block;
}

/* 样式按钮 */

.btn {
  border: none;
  outline: none;
  padding: 12px 16px;
  background-color: #f1f1f1;
  cursor: pointer;
}

/* 鼠标悬停时添加浅灰色背景 */

.btn:hover {
  background-color: #ddd;
}

/* 向活动按钮添加深色背景 */

.btn.active {
  background-color: #666;
  color: white;
}
<!-- language: lang-html -->

<!--筛选按钮-->
<div id="myBtnContainer">
  <button class="btn active" onclick="filterSelection('all')">
    Show all
  </button>
  <button class="btn" onclick="filterSelection('red')">Red</button>
  <button class="btn" onclick="filterSelection('yellow')">Yellow</button>
  <button class="btn" onclick="filterSelection('blue')">Blue</button>
  <button class="btn" onclick="filterSelection('green')">Green</button>
</div>

<!--表格-->
<div id="myModal" class="modal">
  <div class="modal-content">
    <span class="close">&times;</span>
    <p id="modalText"></p>
  </div>
</div>
<div id="container"></div>

<div class="periodic-table filter-container">
  <div class="empty-space-1"></div>
  <div class="empty-space-2"></div>

  <button class="open-modal filterDiv yellow" data-id="h1">
    <div class="periodic-element one">
      <div class="periodic-table-inner">
        <div class="title">h1</div>
        <div class="desc">Large Heading</div>
      </div>
    </div>
  </button>

  <!-- 继续添加其他按钮... -->

</div>
英文:

I want to create a filter where clicking on each box will remove any elements which don't match that description. I have tried adding some code found on W3 but this isn't working for me:
https://www.w3schools.com/howto/howto_js_filter_elements.asp

When I click on the boxes nothing is filtered off the page.

Image of project

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

// Filters
filterSelection(&quot;all&quot;);
function filterSelection(c) {
var x, i;
x = document.getElementsByClassName(&quot;filterDiv&quot;);
if (c == &quot;all&quot;) c = &quot;&quot;;
// Add the &quot;show&quot; class (display:block) to the filtered elements, and remove the &quot;show&quot; class from the elements that are not selected
for (i = 0; i &lt; x.length; i++) {
w3RemoveClass(x[i], &quot;show&quot;);
if (x[i].className.indexOf(c) &gt; -1) w3AddClass(x[i], &quot;show&quot;);
}
}
// Show filtered elements
function w3AddClass(element, name) {
var i, arr1, arr2;
arr1 = element.className.split(&quot; &quot;);
arr2 = name.split(&quot; &quot;);
for (i = 0; i &lt; arr2.length; i++) {
if (arr1.indexOf(arr2[i]) == -1) {
element.className += &quot; &quot; + arr2[i];
}
}
}
// Hide elements that are not selected
function w3RemoveClass(element, name) {
var i, arr1, arr2;
arr1 = element.className.split(&quot; &quot;);
arr2 = name.split(&quot; &quot;);
for (i = 0; i &lt; arr2.length; i++) {
while (arr1.indexOf(arr2[i]) &gt; -1) {
arr1.splice(arr1.indexOf(arr2[i]), 1);
}
}
element.className = arr1.join(&quot; &quot;);
}
// Add active class to the current control button (highlight it)
var btnContainer = document.getElementById(&quot;myBtnContainer&quot;);
var btns = btnContainer.getElementsByClassName(&quot;btn&quot;);
for (var i = 0; i &lt; btns.length; i++) {
btns[i].addEventListener(&quot;click&quot;, function() {
var current = document.getElementsByClassName(&quot;active&quot;);
current[0].className = current[0].className.replace(&quot; active&quot;, &quot;&quot;);
this.className += &quot; active&quot;;
});
}

<!-- language: lang-css -->

/*FILTERS*/
.filter-container {
overflow: hidden;
}
.filterDiv {
float: left;
background-color: #2196f3;
color: #ffffff;
width: 100px;
line-height: 100px;
text-align: center;
margin: 2px;
display: none;
/* Hidden by default */
}
/* The &quot;show&quot; class is added to the filtered elements */
.show {
display: block;
}
/* Style the buttons */
.btn {
border: none;
outline: none;
padding: 12px 16px;
background-color: #f1f1f1;
cursor: pointer;
}
/* Add a light grey background on mouse-over */
.btn:hover {
background-color: #ddd;
}
/* Add a dark background to the active button */
.btn.active {
background-color: #666;
color: white;
}

<!-- language: lang-html -->

&lt;!--FILTER BUTTONS--&gt;
&lt;div id=&quot;myBtnContainer&quot;&gt;
&lt;button class=&quot;btn active&quot; onclick=&quot;filterSelection(&#39;all&#39;)&quot;&gt;
Show all
&lt;/button&gt;
&lt;button class=&quot;btn&quot; onclick=&quot;filterSelection(&#39;red&#39;)&quot;&gt;Red&lt;/button&gt;
&lt;button class=&quot;btn&quot; onclick=&quot;filterSelection(&#39;yellow&#39;)&quot;&gt;Yellow&lt;/button&gt;
&lt;button class=&quot;btn&quot; onclick=&quot;filterSelection(&#39;blue&#39;)&quot;&gt;Blue&lt;/button&gt;
&lt;button class=&quot;btn&quot; onclick=&quot;filterSelection(&#39;green&#39;)&quot;&gt;Green&lt;/button&gt;
&lt;/div&gt;
&lt;!--THE TABLE--&gt;
&lt;div id=&quot;myModal&quot; class=&quot;modal&quot;&gt;
&lt;div class=&quot;modal-content&quot;&gt;
&lt;span class=&quot;close&quot;&gt;&amp;times;&lt;/span&gt;
&lt;p id=&quot;modalText&quot;&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&quot;container&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;periodic-table&quot; class=&quot;filter-container&quot;&gt;
&lt;div class=&quot;empty-space-1&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;empty-space-2&quot;&gt;&lt;/div&gt;
&lt;button class=&quot;open-modal&quot; class=&quot;filterDiv yellow&quot; data-id=&quot;h1&quot;&gt;
&lt;div class=&quot;periodic-element one&quot;&gt;
&lt;div class=&quot;periodic-table-inner&quot;&gt;
&lt;div class=&quot;title&quot;&gt;h1&lt;/div&gt;
&lt;div class=&quot;desc&quot;&gt;Large Heading&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/button&gt;
&lt;button class=&quot;open-modal&quot; class=&quot;filterDiv yellow&quot; data-id=&quot;style&quot;&gt;
&lt;div class=&quot;periodic-element one&quot;&gt;
&lt;div class=&quot;periodic-table-inner&quot;&gt;
&lt;div class=&quot;title&quot;&gt;Style&lt;/div&gt;
&lt;div class=&quot;desc&quot;&gt;Styling&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/button&gt;
&lt;button class=&quot;open-modal&quot; class=&quot;filterDiv blue&quot; data-id=&quot;anchor&quot;&gt;
&lt;div class=&quot;periodic-element two&quot;&gt;
&lt;div class=&quot;periodic-table-inner&quot;&gt;
&lt;div class=&quot;title&quot;&gt;A&lt;/div&gt;
&lt;div class=&quot;desc&quot;&gt;Anchor&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/button&gt;
&lt;button class=&quot;open-modal&quot; class=&quot;filterDiv green&quot; data-id=&quot;comment&quot;&gt;
&lt;div class=&quot;periodic-element three&quot;&gt;
&lt;div class=&quot;periodic-table-inner&quot;&gt;
&lt;div class=&quot;title&quot;&gt;!-&gt;&lt;/div&gt;
&lt;div class=&quot;desc&quot;&gt;Comment&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/button&gt;
&lt;button class=&quot;open-modal&quot; class=&quot;filterDiv green&quot; data-id=&quot;svg&quot;&gt;
&lt;div class=&quot;periodic-element three&quot;&gt;
&lt;div class=&quot;periodic-table-inner&quot;&gt;
&lt;div class=&quot;title&quot;&gt;Svg&lt;/div&gt;
&lt;div class=&quot;desc&quot;&gt;Svg Image&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/button&gt;
&lt;button class=&quot;open-modal&quot; class=&quot;filterDiv red&quot; data-id=&quot;ul&quot;&gt;
&lt;div class=&quot;periodic-element four&quot;&gt;
&lt;div class=&quot;periodic-table-inner&quot;&gt;
&lt;div class=&quot;title&quot;&gt;Ul&lt;/div&gt;
&lt;div class=&quot;desc&quot;&gt;Unordered List&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/button&gt;
&lt;button class=&quot;open-modal&quot; class=&quot;filterDiv red&quot; data-id=&quot;br&quot;&gt;
&lt;div class=&quot;periodic-element four&quot;&gt;
&lt;div class=&quot;periodic-table-inner&quot;&gt;
&lt;div class=&quot;title&quot;&gt;Br&lt;/div&gt;
&lt;div class=&quot;desc&quot;&gt;Line Break&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/button&gt;
&lt;button class=&quot;open-modal&quot; class=&quot;filterDiv yellow&quot; data-id=&quot;li&quot;&gt;
&lt;div class=&quot;periodic-element one&quot;&gt;
&lt;div class=&quot;periodic-table-inner&quot;&gt;
&lt;div class=&quot;title&quot;&gt;Li&lt;/div&gt;
&lt;div class=&quot;desc&quot;&gt;List&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/button&gt;
&lt;button class=&quot;open-modal&quot; class=&quot;filterDiv yellow&quot; data-id=&quot;ol&quot;&gt;
&lt;div class=&quot;periodic-element one&quot;&gt;
&lt;div class=&quot;periodic-table-inner&quot;&gt;
&lt;div class=&quot;title&quot;&gt;Ol&lt;/div&gt;
&lt;div class=&quot;desc&quot;&gt;Ordered List&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/button&gt;
&lt;button class=&quot;open-modal&quot; class=&quot;filterDiv green&quot; data-id=&quot;img&quot;&gt;
&lt;div class=&quot;periodic-element three&quot;&gt;
&lt;div class=&quot;periodic-table-inner&quot;&gt;
&lt;div class=&quot;title&quot;&gt;Img&lt;/div&gt;
&lt;div class=&quot;desc&quot;&gt;Image&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/button&gt;
&lt;button class=&quot;open-modal&quot; class=&quot;filterDiv green&quot; data-id=&quot;nav&quot;&gt;
&lt;div class=&quot;periodic-element three&quot;&gt;
&lt;div class=&quot;periodic-table-inner&quot;&gt;
&lt;div class=&quot;title&quot;&gt;Nav&lt;/div&gt;
&lt;div class=&quot;desc&quot;&gt;Navigation&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/button&gt;
&lt;button class=&quot;open-modal&quot; class=&quot;filterDiv blue&quot; data-id=&quot;em&quot;&gt;
&lt;div class=&quot;periodic-element two&quot;&gt;
&lt;div class=&quot;periodic-table-inner&quot;&gt;
&lt;div class=&quot;title&quot;&gt;Em&lt;/div&gt;
&lt;div class=&quot;desc&quot;&gt;Emphasized&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/button&gt;
&lt;button class=&quot;open-modal&quot; class=&quot;filterDiv blue&quot; data-id=&quot;div&quot;&gt;
&lt;div class=&quot;periodic-element two&quot;&gt;
&lt;div class=&quot;periodic-table-inner&quot;&gt;
&lt;div class=&quot;title&quot;&gt;Div&lt;/div&gt;
&lt;div class=&quot;desc&quot;&gt;Groups Elements&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/button&gt;
&lt;button class=&quot;open-modal&quot; class=&quot;filterDiv blue&quot; data-id=&quot;link&quot;&gt;
&lt;div class=&quot;periodic-element two&quot;&gt;
&lt;div class=&quot;periodic-table-inner&quot;&gt;
&lt;div class=&quot;title&quot;&gt;Link&lt;/div&gt;
&lt;div class=&quot;desc&quot;&gt;External Link&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/button&gt;
&lt;button class=&quot;open-modal&quot; class=&quot;filterDiv red&quot; data-id=&quot;i&quot;&gt;
&lt;div class=&quot;periodic-element four&quot;&gt;
&lt;div class=&quot;periodic-table-inner&quot;&gt;
&lt;div class=&quot;title&quot;&gt;I&lt;/div&gt;
&lt;div class=&quot;desc&quot;&gt;Italic&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/button&gt;
&lt;button class=&quot;open-modal&quot; class=&quot;filterDiv red&quot; data-id=&quot;u&quot;&gt;
&lt;div class=&quot;periodic-element four&quot;&gt;
&lt;div class=&quot;periodic-table-inner&quot;&gt;
&lt;div class=&quot;title&quot;&gt;U&lt;/div&gt;
&lt;div class=&quot;desc&quot;&gt;Underlined&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/button&gt;
&lt;button class=&quot;open-modal&quot; class=&quot;filterDiv red&quot; data-id=&quot;b&quot;&gt;
&lt;div class=&quot;periodic-element four&quot;&gt;
&lt;div class=&quot;periodic-table-inner&quot;&gt;
&lt;div class=&quot;title&quot;&gt;B&lt;/div&gt;
&lt;div class=&quot;desc&quot;&gt;Bold text&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/button&gt;
&lt;button class=&quot;open-modal&quot; class=&quot;filterDiv blue&quot; data-id=&quot;col&quot;&gt;
&lt;div class=&quot;periodic-element two&quot;&gt;
&lt;div class=&quot;periodic-table-inner&quot;&gt;
&lt;div class=&quot;title&quot;&gt;Col&lt;/div&gt;
&lt;div class=&quot;desc&quot;&gt;Column Properties&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/button&gt;
&lt;button class=&quot;open-modal&quot; class=&quot;filterDiv blue&quot; data-id=&quot;data&quot;&gt;
&lt;div class=&quot;periodic-element two&quot;&gt;
&lt;div class=&quot;periodic-table-inner&quot;&gt;
&lt;div class=&quot;title&quot;&gt;Data&lt;/div&gt;
&lt;div class=&quot;desc&quot;&gt;M-R Data&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/button&gt;
&lt;button class=&quot;open-modal&quot; class=&quot;filterDiv green&quot; data-id=&quot;body&quot;&gt;
&lt;div class=&quot;periodic-element three&quot;&gt;
&lt;div class=&quot;periodic-table-inner&quot;&gt;
&lt;div class=&quot;title&quot;&gt;Body&lt;/div&gt;
&lt;div class=&quot;desc&quot;&gt;Main Content&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/button&gt;
&lt;button class=&quot;open-modal&quot; class=&quot;filterDiv green&quot; data-id=&quot;dfn&quot;&gt;
&lt;div class=&quot;periodic-element three&quot;&gt;
&lt;div class=&quot;periodic-table-inner&quot;&gt;
&lt;div class=&quot;title&quot;&gt;Dfn&lt;/div&gt;
&lt;div class=&quot;desc&quot;&gt;Defined term&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/button&gt;
&lt;button class=&quot;open-modal&quot; class=&quot;filterDiv blue&quot; data-id=&quot;title&quot;&gt;
&lt;div class=&quot;periodic-element two&quot;&gt;
&lt;div class=&quot;periodic-table-inner&quot;&gt;
&lt;div class=&quot;title&quot;&gt;Title&lt;/div&gt;
&lt;div class=&quot;desc&quot;&gt;Tab Title&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/button&gt;
&lt;button class=&quot;open-modal&quot; class=&quot;filterDiv blue&quot; data-id=&quot;meta&quot;&gt;
&lt;div class=&quot;periodic-element two&quot;&gt;
&lt;div class=&quot;periodic-table-inner&quot;&gt;
&lt;div class=&quot;title&quot;&gt;Meta&lt;/div&gt;
&lt;div class=&quot;desc&quot;&gt;Metadata&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/button&gt;
&lt;button class=&quot;open-modal&quot; class=&quot;filterDiv yellow data-id=&quot; abbr &quot;&gt;
&lt;div class=&quot;periodic-element one &quot;&gt;
&lt;div class=&quot;periodic-table-inner &quot;&gt;
&lt;div class=&quot;title &quot;&gt;Abbr&lt;/div&gt;
&lt;div class=&quot;desc &quot;&gt;Abbreviation&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/button&gt;
&lt;button class=&quot;open-modal &quot; class=&quot;filterDiv red &quot; data-id=&quot;form &quot;&gt;
&lt;div class=&quot;periodic-element four &quot;&gt;
&lt;div class=&quot;periodic-table-inner &quot;&gt;
&lt;div class=&quot;title &quot;&gt;Form&lt;/div&gt;
&lt;div class=&quot;desc &quot;&gt;Form for User&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/button&gt;
&lt;button class=&quot;open-modal &quot; class=&quot;filterDiv red &quot; data-id=&quot;h6 &quot;&gt;
&lt;div class=&quot;periodic-element four &quot;&gt;
&lt;div class=&quot;periodic-table-inner &quot;&gt;
&lt;div class=&quot;title &quot;&gt;h6&lt;/div&gt;
&lt;div class=&quot;desc &quot;&gt;Small Heading&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/button&gt;
&lt;button class=&quot;open-modal &quot; class=&quot;filterDiv green &quot; data-id=&quot;main &quot;&gt;
&lt;div class=&quot;periodic-element three &quot;&gt;
&lt;div class=&quot;periodic-table-inner &quot;&gt;
&lt;div class=&quot;title &quot;&gt;Main&lt;/div&gt;
&lt;div class=&quot;desc &quot;&gt;Main Content&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/button&gt;
&lt;button class=&quot;open-modal &quot; class=&quot;filterDiv yellow &quot; data-id=&quot;html &quot;&gt;
&lt;div class=&quot;periodic-element one &quot;&gt;
&lt;div class=&quot;periodic-table-inner &quot;&gt;
&lt;div class=&quot;title &quot;&gt;Html&lt;/div&gt;
&lt;div class=&quot;desc &quot;&gt;Root HTML&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/button&gt;
&lt;button class=&quot;open-modal &quot; class=&quot;filterDiv green &quot; data-id=&quot;p &quot;&gt;
&lt;div class=&quot;periodic-element three &quot;&gt;
&lt;div class=&quot;periodic-table-inner &quot;&gt;
&lt;div class=&quot;title &quot;&gt;P&lt;/div&gt;
&lt;div class=&quot;desc &quot;&gt;Paragraph&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/button&gt;
&lt;button class=&quot;open-modal &quot; class=&quot;filterDiv green &quot; data-id=&quot;href &quot;&gt;
&lt;div class=&quot;periodic-element three &quot;&gt;
&lt;div class=&quot;periodic-table-inner &quot;&gt;
&lt;div class=&quot;title &quot;&gt;Href&lt;/div&gt;
&lt;div class=&quot;desc &quot;&gt;URL&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/button&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 3

您的HTML代码存在问题:

  • 这里有一个未闭合的属性:

    &lt;button class=&quot;open-modal&quot; class=&quot;filterDiv yellow data-id=&quot; abbr &quot;&gt;
    

    在"yellow"之后添加缺失的双引号。

  • 您有两个class属性... 先出现的那个将优先。因此,将所有这些重复的属性合并成一个:

    &lt;button class=&quot;open-modal filterDiv yellow&quot; data-id=&quot; abbr &quot;&gt;
    
英文:

Your HTML is not right:

  • There is an unclosed attribute here:

    &lt;button class=&quot;open-modal&quot; class=&quot;filterDiv yellow data-id=&quot; abbr &quot;&gt;
    

    Add the missing double quote after "yellow".

  • You have double class attributes... The earlier one will take precedence. So join all those double attributes into one:

    &lt;button class=&quot;open-modal filterDiv yellow&quot; data-id=&quot; abbr &quot;&gt;
    

答案2

得分: 0

@trincot的修复方法有效,应该被接受为答案。

但是,我想提出一些关于JavaScript代码的改进建议:

// 过滤
filterSelection("all");

// 为选定元素添加“show”类,并从未选中的元素中移除它
function filterSelection(c) {
  let elements = document.querySelectorAll('.filterDiv'); 
  elements.forEach((el) => {
    if (c == 'all' || el.classList.contains(c))
      el.classList.add('show')
    else
      el.classList.remove('show');
  });
}

// w3AddClass和w3RemoveClass不需要

// 为当前控制按钮添加活动类(突出显示)
let btns = document.querySelectorAll("#myBtnContainer .btn");
btns.forEach((btn) => {
  btn.addEventListener("click", () => {
    var current = document.querySelector(".active");
    current.classList.remove('active');
    this.classList.add('active');
  });
});

W3Schools在这里做了很多不必要的工作,可能是为了展示一些语法和语言功能。在这种情况下,重新发明了用于添加和移除类的基本内置功能。

这是一个供学习使用的资源,不是在生产中复制代码的好地方。

英文:

@trincot's fix works, and should be the accepted answer.

but I'd like to suggest some improvements to the javascript code:

// Filters
filterSelection(&quot;all&quot;);

// Adds the &quot;show&quot; class to selected elements, and removes it from unselected
function filterSelection(c) {
  let elements = document.querySelectorAll(&#39;.filterDiv&#39;); 
  elements.forEach((el) =&gt; {
    if( c == &#39;all&#39; || el.classList.contains(c) )
      el.classList.add(&#39;show&#39;)
    else
      el.classList.remove(&#39;show&#39;);
  });
}

// w3AddClass and w3RemoveClass is not needed

// Add active class to the current control button (highlight it)
let btns = document.querySelectorAll(&quot;#myBtnContainer btn&quot;);
btns.forEach((btn) =&gt; {
  btn.addEventListener(&quot;click&quot;, () =&gt; {
    var current = document.querySelector(&quot;active&quot;);
    current.classList.remove(&#39;active&#39;);
    this.classList.add(&#39;active&#39;);
  });
});

W3Schools is doing alot of unnecessary stuff here, probably to show of some syntax and features of the language. In this case reinventing the wheels for basic built-in functions like adding and removing classes.

It's a resource intended for learning, and not a good place to copy code for use in production.

huangapple
  • 本文由 发表于 2023年5月29日 01:57:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76352868.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定