2016-06-16 35 views
0

以下のスクリプトでは、3つの値を動的に置き換える必要があり、動作させることができません。JavaScriptで動的に値を設定する方法はありますか?

値はページ上の要素の文字列として検索できます。

私は、各値のために、スクリプトの最後にこれを追加しようとしました:

ccs_cc_args.push(['MPN', $(".productboxArticlenumber").text()]); 

これはコンソールに正しい値を示し、それは私にこのような何か与える:

[["MFR_NAME", "Apple"], ["MPN", "MLH82N/A"], ["CPN", "244992"]] 

しかし、スクリプト自体は更新されていません - それは何をすべきかを実行しません。

ターゲット要素の文字列からスクリプトを動的に更新するにはどうすればよいですか?

HTML:

<div class="productboxArticlenumber">MLH82N/A</div> 

スクリプト:

var ccs_cc_args = ccs_cc_args || []; 
    ccs_cc_args.push(['cpn', 'CPN']);   ///Dynamically replace the CPN text 
    ccs_cc_args.push(['mf', 'MFR_NAME']);  //Dynamically replace the MFR_NAME text 
    ccs_cc_args.push(['pn', 'MPN']);   //Dynamically replace the MPN text 
    ccs_cc_args.push(['lang', 'nl']); 
    ccs_cc_args.push(['market', 'NL']); 

    (function() { 
    var o = ccs_cc_args; o.push(['_SKey', '3a535060']); o.push(['_ZoneId', '6cddbf26bb']); 
    var sc = document.createElement('script'); sc.type = 'text/javascript'; sc.async = true; 
    sc.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'cdn.cnetcontent.com/jsc/h.js'; 
    var n = document.getElementsByTagName('script')[0]; n.parentNode.insertBefore(sc, n); 
    })(); 

    ccs_cc_args.push(['MPN', $(".productboxArticlenumber").text()]); //added part to push the value dynamic 
+0

されている必要がありますが ' 'CPN''と他の人が働いていないと言うの代わりにこれらの変数を設定することを意味します? – Rohit416

+0

はい、ページにhtmlを追加する予定ですが、ターゲット要素は空です。 1つの製品ページのスクリプトで値を手動で設定すると、そのページのためだけに機能します。上記のように値を動的に追加しようとすると、うまく動作しません。 –

+0

内部に[document.ready()](https://learn.jquery.com/using-)-)を入れたことがありますか?ccs_cc_args.push(['MPN'、$( "productboxArticlenumber")。text()]) jquery-core/document-ready /)? – Rohit416

答えて

0

[OK]を...ので、解決策は簡単だった - 私は間違った値を置き換えます。

ccs_cc_args.push(['MPN', $(".productboxArticlenumber").text()]); 

ccs_cc_args.push(['pn', $(".productboxArticlenumber").text()]); 
関連する問題