繁体   English   中英

在js中向对象添加属性

[英]adding a property to an object in js

我已经用以下代码填充了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();
     });

我想为json_data设置href属性。如果我打印treesList元素,则该数据具有标题但没有href属性。

如何使用js添加它,以便jstree查看并在生成的li元素中填充它? 我尝试过:

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

但是我得到一个不确定的值。

这就是我所拥有的:

Object { title="Iphone3S"}

我得到了:

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

我认为我需要使jstree链接起作用

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

谢谢。

有很多错误,例如:

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

应该:

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

并且以下是语法错误:

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

应该:

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