简体   繁体   English

JS功能隐藏博客文章

[英]JS function to hide blog posts

I created a blog website with 3 main categories: investing, money and retirement. 我创建了一个博客网站,其中包含3个主要类别:投资,金钱和退休。 I added a class element that corresponds to each topic category. 我添加了一个与每个主题类别相对应的类元素。 I'm trying to create JS functions that, when clicked, hide the other two categories (I'm doing with to avoid having a homepage for each category). 我正在尝试创建JS函数,这些函数在单击时会隐藏其他两个类别(我正在这样做是为了避免每个类别都有主页)。 Here is one function to show the Retirement category by hiding the Investing and Money categories: 这是一个通过隐藏“投资”和“金钱”类别来显示“退休”类别的功能:

    function showRetirement() {
        var abc = document.getElementsByClassName("Investing, Money");
        for (var abc2 = 0; abc2 < abc.length; abc2++) {
        abc[abc2].style.display = "none";
            }
        }

Needless to say it doesn't work. 不用说这是行不通的。 Brackets is saying the function "is defined but never used." 方括号表示该功能“已定义但从未使用过”。 It is also saying the var abc " 'document' is not defined." 也说var abc“未定义'文档'。”

This is my link to the function: 这是我对函数的链接:

<div class="nav-link" onclick="showRetirement()">Retirement</div>

Thank you!!! 谢谢!!!

Welcome to SO Dan. 欢迎来到苏丹。

getElementsByClassName does not allow retrieving by multiple class names in one call. getElementsByClassName不允许在一个调用中通过多个类名进行检索。 Instead, it will be easier to use the document.querySelectorAll() method, which allows using query selectors to select items from the page. 相反,使用document.querySelectorAll()方法会更容易,该方法允许使用查询选择器从页面中选择项目。 A query selector to select all items with the class Investing OR Money would look like this: .Investing, .Money 一个查询选择器,用于选择“ InvestingMoney ”类的所有项目,如下所示: .Investing, .Money

As a side note, the warnings in your brackets editor can be ignored at this point, the editor doesn't realise you are calling this function from an onClick="" handler and therefore thinks the function isn't use. 附带说明一下,此时括号编辑器中的警告可以忽略,该编辑器没有意识到您是通过onClick=""处理程序调用此函数的,因此认为该函数未使用。

Another side note, it is conventional to use the variable i in for-loops, other programmers will more easily be able to understand what the variable is used for. 另一个注意事项是,通常在for循环中使用变量i ,其他程序员将更容易理解该变量的用途。

An example solution, using querySelectorAll 使用querySelectorAll的示例解决方案

function showRetirement() {
    var elements = document.querySelectorAll(".Investing, .Money");
    for (var i = 0; i < elements.length; i++) {
        elements[i].style.display = "none";
    }
}

It is also probably worth showing the items with the Retirement class here too, after we hide the others: 在我们隐藏其他项目之后,也可能值得在此处显示“ Retirement类的项目:

function showRetirement() {
    var elementsToHide = document.querySelectorAll(".Investing, .Money");
    for (var i = 0; i < elementsToHide.length; i++) {
        elementsToHide[i].style.display = "none";
    }

    var elementsToShow = document.querySelectorAll(".Retirement");
    for (var i = 0; i < elementsToShow.length; i++) {
        elementsToShow[i].style.display = "block";
    }
}
function showRetirement(){
 var ar_el;
 ar_el = document.getElementsByClassName("Investing");
 for (var i = 0; i < ar_el.length; i++)
  ar_el[i].style.display = "none";

 ar_el = document.getElementsByClassName("Money");
 for (var i = 0; i < ar_el.length; i++)
  ar_el[i].style.display = "none";

 ar_el = document.getElementsByClassName("Retirement");
 for (var i = 0; i < ar_el.length; i++)
  ar_el[i].style.display = "block";

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM