简体   繁体   English

Javascript创建类别超链接数组

[英]Javascript Create Array of Category Hyperlinks

I have an array of sports items. 我有一系列体育用品。 I am trying to build an array of category names from the main array. 我正在尝试从主数组构建一个类别名称数组。 Each category name is then created as a hyperlink element which onclick triggers a function that loads the associated products. 然后,将每个类别名称创建为一个超链接元素,该元素将onclick触发加载相关产品的功能。 I also need to display one instance of the category name as there are multiple. 我还需要显示类别名称的一个实例,因为有多个实例。

When I click the button and call filterCategory(), nothing is getting displayed. 当我单击按钮并调用filterCategory()时,什么都没有显示。 The error I am getting in Chrome Developer Tools is when I call: 我在Chrome开发人员工具中遇到的错误是在致电时:

document.getElementById("brand-category").appendChild(categoriesLink);

... the error on this line is: ...此行的错误是:

failed to execute appendChild on 'Node': paramater 1 is not type Node 无法在“节点”上执行appendChild:参数1不是节点类型

Here is what I have tried so far: 到目前为止,这是我尝试过的:

var data = {
"Nike": [
    {
    "id": "1",
    "brand":"Nike",
    "productTitle": "Black Hoody",
    "productImage": "image.jpg",
    "category": "Hoodies",
    "priceBand": "1103", 
    "salePrice": "120.00"},
     {
    "id": "2",
    "brand":"Nike",
    "productTitle": "Running Jacket",
    "productImage": "image.jpg",
    "category": "Jackets",
    "priceBand": "1104", 
    "salePrice": "150.00"}
],

"Sketchers": [
    {
    "id": "3",
    "brand":"Sketchers",
    "productTitle": "Running Shoes Style 1",
    "productImage": "image.jpg",
    "category": "Running Shoes",
    "priceBand": "1103", 
    "salePrice": "120.00"},
     {
    "id": "4",
    "brand":"Sketchers",
    "productTitle": "Running Shoes Style 2",
    "productImage": "image.jpg",
    "category": "Running Shoes",
    "priceBand": "1102", 
    "salePrice": "90.00"}

]}  

function filterCategory() {
//build an array of unique category names from the main array.  
//create an element and assign the category name as a link
//on click, load the associated products from that category

var categories, categoriesLink;

for(categories in data) {
    categoriesLink = document.createElement("a");
    categoriesLink.innerHTML = categories;
    categoriesLink.categories_Arr=data[categories];
        categoriesLink = function() {
            var catsContainer=document.getElementById('brand-category');
            var categories_Arr=this.categories_Arr, i, I=categories_Arr.length, item;
            var catOutput="";
            for(i=0; i<I; I++) 
            {
                item=categories_Arr[i];
                 catOutput+= "<img src=\""+item.logo+"\"/>" +  item.id + item.productTitle + item.productDescription + "<img src=\""+item.productImage+"\"/>" + item.rrp + item.salePrice + "<br>";
             }

            catsContainer.innerHTML=catOutput;
            catsContainer.style.display="block";
            return false;
        }

        document.getElementById("brand-category").appendChild(categoriesLink);
    }
}

<div id='cat'><button id='filterCategory' name='filterCategory' value='Category' onclick="filterCategory();">Category</button></div>


<div id="brand-category"></div>

Any help much appreciated. 任何帮助,不胜感激。

Cheers 干杯

"categoriesLink = function()" , you trying to append function on your html. “ categoriesLink = function()”,您尝试在HTML上附加函数。 That's why is throwing error "parameter 1 is not of node type" either you can change the function name or if you want you can use append to show whole function which is not good. 这就是为什么会引发错误“参数1不是节点类型”的原因,或者您可以更改函数名称,或者如果您想使用append来显示整个函数,那是不好的。 see below snippet 见下面的片段

 var data = { "Nike": [ { "id": "1", "brand":"Nike", "productTitle": "Black Hoody", "productImage": "image.jpg", "category": "Hoodies", "priceBand": "1103", "salePrice": "120.00"}, { "id": "2", "brand":"Nike", "productTitle": "Running Jacket", "productImage": "image.jpg", "category": "Jackets", "priceBand": "1104", "salePrice": "150.00"} ], "Sketchers": [ { "id": "3", "brand":"Sketchers", "productTitle": "Running Shoes Style 1", "productImage": "image.jpg", "category": "Running Shoes", "priceBand": "1103", "salePrice": "120.00"}, { "id": "4", "brand":"Sketchers", "productTitle": "Running Shoes Style 2", "productImage": "image.jpg", "category": "Running Shoes", "priceBand": "1102", "salePrice": "90.00"} ]} function filterCategory() { //build an array of unique category names from the main array. //create an element and assign the category name as a link //on click, load the associated products from that category var categories, categoriesLink; for(categories in data) { categoriesLink = document.createElement("a"); categoriesLink.innerHTML = categories; categoriesLink.setAttribute('href', ''); categoriesLink.categories_Arr=data[categories]; var categoriesLinks = function() { var catsContainer=document.getElementById('brand-category'); var categories_Arr=this.categories_Arr, i, I=categories_Arr.length, item; var catOutput=""; for(i=0; i<I; I++) { item=categoreis_Arr[i]; catOutput+= "<img src=\\""+item.logo+"\\"/>" + item.id + item.productTitle + item.productDescription + "<img src=\\""+item.productImage+"\\"/>" + item.rrp + item.salePrice + "<br>"; } catsContainer.innerHTML=catOutput; catsContainer.style.display="block"; return false; } document.getElementById("brand-category").appendChild(categoriesLink); } } 
 <div id='cat'><button id='filterCategory' name='filterCategory' value='Category' onclick="filterCategory();">Category</button></div> <div id="brand-category"></div> 

Answer to your side question is what you doing is, in your for each loop you extract the key from object and assign it to innerHTML. 回答您的另一个问题是您在做什么,在每个循环中,您从对象中提取键并将其分配给innerHTML。 So instead of doing categoriesLink.innerHTML = categories; 因此,与其做categoriesLink.innerHTML = categories; you should do something like that : categoriesLink.innerHTML = data[categories]["0"].category; 您应该执行以下操作: categoriesLink.innerHTML = data[categories]["0"].category;

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

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