簡體   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