我有代码来获取已存储在Cookie中的GA参数,并将其作为输入的值以不起作用的形式生效,我无法为我的生活找出原因。
(function( $ ) {
if (Cookies.get('gaParams')) {
var params = Cookies.get('gaParams');
}
$('#source').val(params['utm_source'] ? params['utm_source'] : 'organic');
$('#medium').val(params['utm_medium'] ? params['utm_medium'] : '');
$('#campaign').val(params['utm_campaign'] ? params['utm_campaign'] : '');
$('#keywords').val(params['utm_terms'] ? params['utm_terms'] : '');
console.log(params,params['utm_medium'],params.utm_medium);
}(jQuery));
你可以通过here然后点击"请求引用"来查看它的实际效果。按钮。
console.log将返回{"utm_medium":"testing","utm_source":"whatever"} undefined undefined
我不明白为什么调用对象键不会以这种方式给我这些值。
答案 0 :(得分:1)
因为变量<script type="text/javascript">
$(document).ready(function(){
$('#p1','#p2', '#p3','#p4').hide();
$('#p1-show','#p2-show','#p3-show','#p4-show').click(function(){
$('#p1','#p2','#p3','#p4').show();
});
$('#p1-hide','#p2-hide','#p3-hide','#p4-hide').click(function(){
$('#p1','#p2','#p3','#p4').hide();
});
});
</script>
是字符串类型。您应该使用此方法params
来解析JSON字符串
JSON.parse()