如何在javascript中动态设置值?

时间:2016-06-16 06:24:48

标签: javascript jquery

我有一个脚本,我动态地需要替换三个值,我无法让它工作。

可以在页面上的元素中查找值作为字符串。

我尝试在脚本末尾为每个值添加:

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 

1 个答案:

答案 0 :(得分:0)

好的..所以解决方案很简单 - 我替换了错误的值。

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

应该是

ccs_cc_args.push(['pn', $(".productboxArticlenumber").text()]);
相关问题