【问题标题】:Find all parents elements in a Json file using jQuery使用 jQuery 查找 Json 文件中的所有父元素
【发布时间】:2015-04-29 07:36:02
【问题描述】:

我目前正在开发一个基于 jQuery 的递归菜单,它看起来已经相当不错了。

包含菜单的 JSon 文件结构如下:

[
   { 
       "Id": "menuOfficeWebControlsForWebApplication", 
       "Title": "Office Web Controls", 
       "Resource": "/Documentation/Data/index.html" },
   { 
       "Id": "menuGettingStarted", 
       "Title": "Getting Started", 
       "Resource": "/Documentation/Data/getting-started.html", 
       "Categories": [{ 
             "Id": "menuCompilingFromSource", 
             "Title": "Compiling From Source", 
             "Resource": "/Documentation/Data/Getting-Started/compiling-from-source.html" 
          },{ 
             "Id": "menuDownloadReleasePackage", 
             "Title": "Download Release Package", 
             "Resource": "/Documentation/Data/Getting-Started/downloading-release-package.html"
          },{ 
             "Id": "menuBuildingYourFirstApplication", 
             "Title": "Building your first application", 
             "Resource": "/Documentation/Data/Getting-Started/building-your-first-application.html" 
        }]
   }
]

现在,我可以使用 jQuery 从菜单中检索一个项目,结果可能是这个项目:

{ 
   "Id": "menuBuildingYourFirstApplication", 
   "Title": "Building your first application", 
   "Resource": "/Documentation/Data/Getting-Started/building-your-first-application.html" 
}

现在,我想检索更高级别的所有元素以及该项目正下方的所有项目。

非常感谢任何帮助。

【问题讨论】:

  • 什么是父母?任何不是当前节点的节点?你能改变节点吗(例如添加 ParentNode 属性)
  • 我可以那样做,但很容易出现拼写错误。父级是层次结构中较高的元素。

标签: javascript jquery css json


【解决方案1】:

JQuery 用于从当前文档的 DOM 中查询 HTML 元素,而不是用于遍历对象或 JSON 表达式字符串。

在任何情况下,给定一个对象,都无法“发现”任何可能持有对它的引用的对象、变量或数组。

我建议您阅读一本不错的 Javascript 书籍并熟悉基础知识。也许一开始就远离像 JQuery 这样的东西,因为它们会让你感到困惑。

【讨论】:

  • 感谢您的回答。我现在做,现在不可能。
  • > "[jQuery is] 不用于遍历对象或 JSON 表达式字符串。"然而,它有许多实用方法可以做到这一点,从 $.parseJson$.each,再到 $.filter$.map 等。然而,像 underscore/lodash 这样的库在处理对象时会更有用。
猜你喜欢
  • 1970-01-01
  • 2011-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-20
  • 1970-01-01
  • 2012-01-01
  • 1970-01-01
相关资源
最近更新 更多