1. <tfoot id='DCZUk'></tfoot>
    2. <legend id='DCZUk'><style id='DCZUk'><dir id='DCZUk'><q id='DCZUk'></q></dir></style></legend>

        <bdo id='DCZUk'></bdo><ul id='DCZUk'></ul>
      <i id='DCZUk'><tr id='DCZUk'><dt id='DCZUk'><q id='DCZUk'><span id='DCZUk'><b id='DCZUk'><form id='DCZUk'><ins id='DCZUk'></ins><ul id='DCZUk'></ul><sub id='DCZUk'></sub></form><legend id='DCZUk'></legend><bdo id='DCZUk'><pre id='DCZUk'><center id='DCZUk'></center></pre></bdo></b><th id='DCZUk'></th></span></q></dt></tr></i><div id='DCZUk'><tfoot id='DCZUk'></tfoot><dl id='DCZUk'><fieldset id='DCZUk'></fieldset></dl></div>

        <small id='DCZUk'></small><noframes id='DCZUk'>

        Javascript - 遍历元素的递归函数

        Javascript - Recursive function to iterate through elements(Javascript - 遍历元素的递归函数)

          <i id='Q0uvB'><tr id='Q0uvB'><dt id='Q0uvB'><q id='Q0uvB'><span id='Q0uvB'><b id='Q0uvB'><form id='Q0uvB'><ins id='Q0uvB'></ins><ul id='Q0uvB'></ul><sub id='Q0uvB'></sub></form><legend id='Q0uvB'></legend><bdo id='Q0uvB'><pre id='Q0uvB'><center id='Q0uvB'></center></pre></bdo></b><th id='Q0uvB'></th></span></q></dt></tr></i><div id='Q0uvB'><tfoot id='Q0uvB'></tfoot><dl id='Q0uvB'><fieldset id='Q0uvB'></fieldset></dl></div>
            • <legend id='Q0uvB'><style id='Q0uvB'><dir id='Q0uvB'><q id='Q0uvB'></q></dir></style></legend>
                <tbody id='Q0uvB'></tbody>

                <bdo id='Q0uvB'></bdo><ul id='Q0uvB'></ul>

                <small id='Q0uvB'></small><noframes id='Q0uvB'>

              • <tfoot id='Q0uvB'></tfoot>
                1. 本文介绍了Javascript - 遍历元素的递归函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我刚刚开始阅读 JavaScript,我正在尝试编写一个小型递归函数,该函数将搜索给定节点并以字符串形式返回值列表.

                  I've just started reading up on JavaScript and I'm trying to write a small recursive function that would search through given nodes and return a list of values as a string.

                  我的 HTML 结构可能类似于

                  My HTML structure could be something like

                  <div id="parentfolder">parentfolder1
                      <div class ="item1">item1</div>
                      <div class ="item2">item2</div>
                      <div id="parentfolder">parentfolder2
                          <div class ="item1">item1</div>
                          <div class ="item2">item2</div>
                      </div>
                  </div>
                  

                  这是我的 Javascript 函数:

                  And Here is my Javascript function:

                  function jsoncreator(parentfolderclass){
                      var jstring = '';
                  
                      //get first occurance of parent folder
                      var parentfolder = document.getElementById(parentfolderclass);
                      var childnodes = parentfolder.childNodes;
                  
                      for (property in childnodes){
                          jstring += property+ childnodes[property];
                          if(childnodes[property] === parentfolderclass){
                              jsoncreator(parentfolderclass);
                              jstring += childnodes[property].value + '<br>';
                          }
                          else{
                              //jstring += childnodes[i].value + '<br>';
                          }
                      }
                      document.write(jstring);
                  }
                  

                  我回来的只是

                  0[object Text]1[object HTMLDivElement]2[object Text]3[object HTMLDivElement]4[object Text]5[object HTMLDivElement]6[object Text]length7itemfunction item() { [native code] }
                  

                  当我尝试打印子节点值时,我得到一堆未定义的返回.

                  When I try to print the childnodes values, I get a bunch of undefined returns.

                  如果有人能解释我做错了什么,我将不胜感激.

                  If anybody could explain what I'm doing wrong, I'd really appreciate it.

                  推荐答案

                  你需要做如下的事情(递归跨浏览器)

                  You will need to do something like the following (recursive cross-browser)

                  Javascript

                  function walkTheDOM(node, func) {
                      func(node);
                      node = node.firstChild;
                      while (node) {
                          walkTheDOM(node, func);
                          node = node.nextSibling;
                      }
                  }
                  
                  function textNodeValuesToArray(node) {
                      if (typeof node === "string") {
                          node = document.getElementById(node);
                      }
                  
                      var arrayOfText = [];
                  
                      function pushText(currentNode) {
                          if (currentNode.nodeType === 3) {
                              arrayOfText.push(currentNode.nodeValue);
                          }
                      }
                  
                      walkTheDOM(node, pushText);
                  
                      return arrayOfText;
                  }
                  
                  console.log(textNodeValuesToArray("parentfolder"));
                  

                  关于 jsfiddle

                  或使用 treewalker

                  浏览器兼容性

                  IE9+、FF2+、Chrome 1+、Safari 3+、Opera 9+支持

                  Supported by IE9+, FF2+, Chrome 1+, Safari 3+, Opera 9+

                  Javascript

                  function textNodeValuesToArray(node) {
                      if (typeof node === "string") {
                          node = document.getElementById(node);
                      }
                  
                      var arrayOfText = [],
                          treeWalker = document.createTreeWalker(node, NodeFilter.SHOW_TEXT, {
                              acceptNode: function (node) {
                                  return NodeFilter.FILTER_ACCEPT;
                              }
                          }, false);
                  
                      while (treeWalker.nextNode()) {
                          arrayOfText.push(treeWalker.currentNode.nodeValue);
                      }
                  
                      return arrayOfText;
                  }
                  
                  console.log(textNodeValuesToArray("parentfolder"));
                  

                  关于 jsfiddle

                  没有递归和跨浏览器会是这样的

                  Without recursion and cross browser would be something like this

                  Javascript

                  避免使用标签

                  标签在 JavaScript 中不是很常用,因为它们使程序更难阅读和理解.尽量避免使用标签,根据情况,更喜欢调用函数或抛出错误.

                  Labels are not very commonly used in JavaScript since they make programs harder to read and understand. As much as possible, avoid using labels and, depending on the cases, prefer calling functions or throwing an error.

                  function walkDOM(root, func) {
                      var node = root;
                  
                      start: while (node) {
                          func(node);
                          if (node.firstChild) {
                              node = node.firstChild;
                              continue start;
                          }
                  
                          while (node) {
                              if (node === root) {
                                  break start;
                              }
                  
                              if (node.nextSibling) {
                                  node = node.nextSibling;
                                  continue start;
                              }
                  
                              node = node.parentNode;
                          }
                      }
                  }
                  
                  function textNodeValuesToArray(node) {
                      if (typeof node === "string") {
                          node = document.getElementById(node);
                      }
                  
                      var arrayOfText = [];
                  
                      function pushText(currentNode) {
                          if (currentNode.nodeType === 3) {
                              arrayOfText.push(currentNode.nodeValue);
                          }
                      }
                  
                      walkDOM(node, pushText);
                  
                      return arrayOfText;
                  }
                  
                  console.log(textNodeValuesToArray("parentfolder"));
                  

                  关于 jsfiddle

                  这篇关于Javascript - 遍历元素的递归函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                  CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                  Ordinals in words javascript(javascript中的序数)
                  getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)
                  How do I make a TextGeometry multiline? How do I put it inside a square so it wraps like html text does inside a div?(如何制作 TextGeometry 多线?如何将它放在一个正方形内,以便它像 html 文本一样包裹在 div 内?) - IT屋-程序员软件开发技术分享社
                  How to use coffeescript in developing web-sites?(如何在开发网站时使用coffeescript?)
                  <i id='DG5Lj'><tr id='DG5Lj'><dt id='DG5Lj'><q id='DG5Lj'><span id='DG5Lj'><b id='DG5Lj'><form id='DG5Lj'><ins id='DG5Lj'></ins><ul id='DG5Lj'></ul><sub id='DG5Lj'></sub></form><legend id='DG5Lj'></legend><bdo id='DG5Lj'><pre id='DG5Lj'><center id='DG5Lj'></center></pre></bdo></b><th id='DG5Lj'></th></span></q></dt></tr></i><div id='DG5Lj'><tfoot id='DG5Lj'></tfoot><dl id='DG5Lj'><fieldset id='DG5Lj'></fieldset></dl></div>

                    <small id='DG5Lj'></small><noframes id='DG5Lj'>

                      1. <tfoot id='DG5Lj'></tfoot>
                          <bdo id='DG5Lj'></bdo><ul id='DG5Lj'></ul>
                            <legend id='DG5Lj'><style id='DG5Lj'><dir id='DG5Lj'><q id='DG5Lj'></q></dir></style></legend>

                              <tbody id='DG5Lj'></tbody>