简体   繁体   English

在js中向对象添加属性

[英]adding a property to an object in js

I have jstree populated with this code: 我已经用以下代码填充了jstree:

jQuery(function($) {
        var treesList = {!trees};
        for(j=0; j<treesList.length; j++) {
            console.log(treesList[j]);
            $("#jstree"+treesList[j].attr.promotion_item).jstree({ // here we define the tree structure
                "core" : { "animation" : 200 },
                "json_data" : { "data" : treesList[j]}, 
                "themes" : {"dots" : false, "theme" : "classic"},
                "types" : { "types" : {
                    "default" : { "icon" : { "image" : null } },
                } },
                "plugins" : [ "themes", "json_data" , "types"]
            });  
        }
        rerenderTable();
     });

I want to set the href attribute for the json_data If I print the treesList element, I get that data has a title but not an href attribute. 我想为json_data设置href属性。如果我打印treesList元素,则该数据具有标题但没有href属性。

How can I add it with js in order for jstree to view it and populate it in the generated li element? 如何使用js添加它,以便jstree查看并在生成的li元素中填充它? I've tried with: 我尝试过:

treesList[j].data.attr = "";
treesList[j].data.attr.href = "link";

but I get an undefined value. 但是我得到一个不确定的值。

This is what I have: 这就是我所拥有的:

Object { title="Iphone3S"}

What I got: 我得到了:

Object { title="Iphone3S", attr=""}

What I think I need to makethe jstree link to work 我认为我需要使jstree链接起作用

Object { title="Iphone3S", attr= {href="link"}}

Thanks. 谢谢。

There are many error, like: 有很多错误,例如:

treesList[j].data.attr = "";
treesList[j].data.attr.href = "link";

should be: 应该:

treesList[j].data.attr = {};
treesList[j].data.attr.href = "link";

and the following is a syntax error: 并且以下是语法错误:

Object { title="Iphone3S", attr= {href="link"}}

should be: 应该:

Object { title="Iphone3S", attr: {href : "link"}}

尝试这个,

treesList[j].data.attr= {href:"link"};

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

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