2015-04-29 114 views
-4

我目前正在構建一個基於jQuery的遞歸菜單,該菜單已經很好看了。使用jQuery在Json文件中查找所有父母元素

包含菜單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" 
} 

現在,我想檢索所有更高級別的元素以及該項目正下方的所有項目。

任何幫助,高度讚賞。

+0

什麼是父母?任何不是當前節點的節點?你是否可以改變節點(例如添加一個ParentNode屬性) –

+0

我可以那樣做,但是很容易出現輸入錯誤。父母是層級中較高的元素。 – Complexity

回答

3

JQuery用於從當前文檔的DOM中查詢HTML元素,而不是遍歷對象或JSON表達式字符串。

在任何情況下,給定一個對象,沒有辦法「發現」任何對象,變量或數組,可能會引用它。

我建議你拿起一本體面的Javascript書並熟悉基本知識。起初可能遠離JQuery之類的東西,因爲它們可能會讓你感到困惑。

+0

感謝您的回答。我現在做現在不可能。 – Complexity

+0

>「[jQuery不適用於遍歷對象或JSON表達式字符串。」 然而,它有很多實用的方法可以做到這一點,從'$ .parseJson'到'$ .each',到'$ .filter','$ .map'等。然而像下劃線/ lodash這樣的庫會更有用當使用對象時。 – pawel