0

入れ子になったデータから DOM 要素を探しています。このようなもの

$("div").data( "test", { 
    first: 16,
    last: "pizza!"
});

これは、同様の種類のフォーマットを作成するための私の努力です。しかし、RefAttrValue 変数は変数として動作していません。

for (var i = 0; i < reference.attributes.length; i++) {
    var RefAttrValue = (reference.attributes.item(i).name);
    $(tableCaption).data("referenceData").RefAttrValue = reference.attributes.item(i).value;
}

ここで、「参照」の値は XML です。以下のような値を持つか、同様の XML を含めることができます。

<facilityreference Name="qtitem_fac" AttrTable='table1' colVal='table1colValue'></facilityreference>

data()この下の形式「動的」で形式を設定したいと思います。

$("div").data("test",{ Name: "qtitem_fac", last: "table1colValue" });

助けてください。

4

3 に答える 3

2

私が理解している限り、次のようなものが必要です。

var datObj = {};
for (var i = 0; i < reference.attributes.length; i++) {
    var RefAttrValue = (reference.attributes.item(i).name);
    datObj[RefAttrValue] = reference.attributes.item(i).value;
}
$(tableCaption).data("referenceData", datObj );

$(tableCaption).data("referenceData", {...})あなたのコードにはいくつかの問題があります: 最初:コードを実行する前に何もしないと、行$(tableCaption).data("referenceData")は未定義を返し、例外が発生します。

変数にフィールド名がある場合(RefAttrValueあなたの場合)、次のようなことをする必要があります$(tableCaption).data("referenceData")[RefAttrValue]。そのような表記は同義語ですRefAttrValue = "Name"$(tableCaption).data("referenceData").Name

これを行う$(tableCaption).data("referenceData").RefAttrValueには、 name で referenceData のプロパティにアクセスするだけですRefAttrValue

于 2013-01-25T13:00:02.243 に答える
1

未検証:

 obj = {};

    $('facilityreference').each(function(){
       $.each(this.attributes, function(i, attrib){
           obj[attrib.name] = attrib.value;
      });

    }); // then do something with you object
于 2013-01-25T13:03:17.483 に答える
-1
use this...Hopefully this will solve the requirement.!
str = $('#id').data('test');var json = JSON.stringify(eval("(" + str + ")"));alert(json);
于 2013-01-25T13:06:02.307 に答える