提问者:小点点

如何获取div的子级


用户可以通过按下一个按钮来选择一个特定的感兴趣的主题。 当这种情况发生时,各种div将变得可见或不可见,这取决于该div是否有引用该主题的链接。

null

function GetPostsByTopic(topic) {
  var area = document.getElementById("postArea");
  var topicAreas = area.getElementsByClassName("topicArea");

  for (i = 0; i < topicAreas.length; i++) {
    var children = topicAreas[i].children;

    var topics = [];
    for (j = 0; j < children.length; j++) {
      topics.push(children[j].getAttribute("asp-route-name"));
      document.getElementById("firstTest").innerHTML = children[j].toString();
    }

    var b = topics.includes(topic);
    if (b == true) {
      var parentId = document.getElementById(topicAreas[i]).parentNode.id;
      document.getElementById(parent).style.display = 'block';
    } else {
      document.getElementById(parent).style.display = 'none';
    }
  }

}
<div class="topicBox">
  <button class="topicButton" onclick="GetPostsByTopic('Pets')">Pets</button>
  <button class="topicButton" onclick="GetPostsByTopic('Vacation')">Vacation</button>
</div>

<div id="postArea">
  <div class="post" id="post1">
    <div class="topicArea">
      <a asp-action="Topic" asp-route-name="Pets">Pets</a>
    </div>
  </div>
  <div class="post" id="post2">
    <div class="topicArea">
      <a asp-action="Topic" asp-route-name="Vacation">Vacation</a>
    </div>
  </div>
  <div class="post" id="post3">
    <div class="topicArea">
      <a asp-action="Topic" asp-route-name="Pets">Pets</a>
    </div>
  </div>
</div>

null

据我所知,麻烦开始于JS部分的早期。 我可以看到,当dovar children=topicareals[I].childress时,我什么也得不到。


共2个答案

匿名用户

孩子不是问题所在。 当您运行代码时,您会得到错误“Uncapted TypeError:Cannot set property'innerhtml'of Null”。 查看使用.innerHTML的代码,我们会发现您正在尝试引用此代码中没有的元素:

document.getElementById("firstTest")

现在,在添加了这些之后,您仍然有一些项目需要更改。

  • asp-actionasp-route-name是无效的HTML。 您使用的框架是否需要此语法?
  • 不要使用.GetElementsByClassName().
  • 对结果使用.QuerySelectorAll()Array.Foreach(),以便于循环。
  • 在不使用HTML字符串时不要使用.innerHTML,因为这样做会影响安全性和性能。 尽可能避免内联样式。 使用它们会导致代码重复,并且代码更难扩展。 相反,使用CSS类和.classListAPI.

点击按钮时会发生什么还不是很清楚,但是请看下面更新的代码:

null

function GetPostsByTopic(topic) {
    var area = document.getElementById("postArea");
    // Don't use .getElementsByClassName() as it provides a live node list
    // and causes quite a performance hit, especially when used in loops.
    // Use .querySelectorAll() and then use .forEach() on the collection that
    // it returns to iterate over them.
    area.querySelectorAll(".topicArea").forEach(function(area){
        var topics = [];
        // No need for children, here. Again, use .querySelectorAll()
        area.querySelectorAll("*").forEach(function(child) {   
          topics.push(child.getAttribute("asp-route-name"));
          document.getElementById("firstTest").textContent = child.getAttribute("asp-route-name");
        });
       
        if (topics.indexOf(topic) > -1) {
          // Don't use inline styles if you can avoid it.
          // Instead use pre-made classes.
          area.classList.add("hidden");
        }
        else {
          area.classList.remove("hidden");
        }
    });
}
/* Use CSS classes when possible instead of inline styles */
.hidden { display:none; }
<div class="topicBox">
        <button class="topicButton" onclick="GetPostsByTopic('Pets')">Pets</button>
        <button class="topicButton" onclick="GetPostsByTopic('Vacation')">Vacation</button>
</div>
<div id="postArea">
        <div class="post" id="post1">     
            <div class="topicArea">
                <a asp-action="Topic" asp-route-name="Pets">Pets</a>
            </div>     
        </div>
        <div class="post" id="post2">
            <div class="topicArea">
                <a asp-action="Topic" asp-route-name="Vacation">Vacation</a>
            </div>
        </div>
        <div class="post" id="post3">
            <div class="topicArea">
                <a asp-action="Topic" asp-route-name="Pets">Pets</a>
            </div>
        </div>      
</div>

<div id="firstTest"></div>

匿名用户

我希望这就是你想要做的。 根据您单击的按钮,将显示相应的div。

null

function GetPostsByTopic(topic) {
    var area = document.getElementById("postArea");
    var topicAreas = area.getElementsByClassName("topicArea");
    for (i = 0; i < topicAreas.length; i++) {
        var children = topicAreas[i].children;
        for (j = 0; j < children.length; j++) {
           var parentId = topicAreas[i].parentNode.id;
           if(children[j].getAttribute("asp-route-name") === topic){
            document.getElementById(parentId).style.display = 'block';
           }else{
            document.getElementById(parentId).style.display = 'none';
           }
        }
    }
}
<div class="topicBox">
  <button class="topicButton" onclick="GetPostsByTopic('Pets')">Pets</button>
  <button class="topicButton" onclick="GetPostsByTopic('Vacation')">Vacation</button>
</div>

<div id="postArea">
  <div class="post" id="post1">
    <div class="topicArea">
      <a asp-action="Topic" asp-route-name="Pets">Pets</a>
    </div>
  </div>
  <div class="post" id="post2">
    <div class="topicArea">
      <a asp-action="Topic" asp-route-name="Vacation">Vacation</a>
    </div>
  </div>
  <div class="post" id="post3">
    <div class="topicArea">
      <a asp-action="Topic" asp-route-name="Pets">Pets</a>
    </div>
  </div>
</div>