Javascript 我该如何定位我';I’我想用GPT刷新

Javascript 我该如何定位我';I’我想用GPT刷新,javascript,jquery,google-dfp,Javascript,Jquery,Google Dfp,我在页面的页眉中定义了所有的ADSlot,在JavaScript中,我想根据这些ADSlot在页面上的位置刷新其中的“一些” 以下是我目前的情况: googletag.cmd.push(function() { slot1 = googletag.defineSlot('/1234/example', [[728, 90], 'gpt-divId1') .addService(googletag.pubads()); slot2 = googletag.defineSlot('/

我在页面的页眉中定义了所有的ADSlot,在JavaScript中,我想根据这些ADSlot在页面上的位置刷新其中的“一些”

以下是我目前的情况:

googletag.cmd.push(function() {
  slot1 = googletag.defineSlot('/1234/example', [[728, 90], 'gpt-divId1')
    .addService(googletag.pubads());
  slot2 = googletag.defineSlot('/1234/example', [[300, 250], 'gpt-divId2')
    .addService(googletag.pubads());
  slot3 = googletag.defineSlot('/1234/example', [[300, 250], 'gpt-divId3')
    .addService(googletag.pubads());
googletag.pubads().enableSingleRequest();
googletag.pubads().collapseEmptyDivs();
googletag.enableServices();
然后,我将该已定义插槽的名称放入每个div的
数据gpt slot
属性中,例如:

在一个单独的JavaScript中,我尝试运行我的DOM,获取某个区域内所有
数据gpt槽
值的列表,并将它们添加到
googletag.pubads().refresh()

如果我手动调用
googletag.pubads().refresh([slot1,slot2])
,这些广告就会刷新,但如果我试图通过jQuery构建该字符串,它就不会播放

  var gptdivs = [];
  $('.myContainer .gpt-holder').each(function(){
    gptdivs.push($(this).attr('data-gpt-slot'));
  });
  googletag.pubads().refresh([gptdivs]);

我怀疑我遗漏了
$(this).attr('data-gpt-slot')
slot1
slot2
,等等的一些属性?

我认为您这里有一个深层数组,导致了您的问题

refresh([gptdivs])
试试看

refresh(gptdivs)

我认为你有一个很深的阵营,导致了你的问题

refresh([gptdivs])
试试看

refresh(gptdivs)

我认为你有一个很深的阵营,导致了你的问题

refresh([gptdivs])
试试看

refresh(gptdivs)

我认为你有一个很深的阵营,导致了你的问题

refresh([gptdivs])
试试看

refresh(gptdivs)

问题是您试图错误地使用刷新方法。它将实际的ad单元实例作为参数,而不仅仅是它的名称作为字符串

请尝试以下方法:

var adunits = {};

adunits['slot1'] = googletag.defineSlot('/1234/example', [[728, 90], 'gpt-divId1').addService(googletag.pubads());
adunits['slot2'] = googletag.defineSlot('/1234/example', [[300, 250], 'gpt-divId2').addService(googletag.pubads());
adunits['slot3'] = googletag.defineSlot('/1234/example', [[300, 250], 'gpt-divId3').addService(googletag.pubads());
然后,刷新成人的代码变为:

var gptdivs = [];
$('.myContainer .gpt-holder').each(function(){
    gptdivs.push(adunits[$(this).attr('data-gpt-slot')]);
});
googletag.pubads().refresh(gptdivs);

问题是您试图错误地使用刷新方法。它将实际的ad单元实例作为参数,而不仅仅是它的名称作为字符串

请尝试以下方法:

var adunits = {};

adunits['slot1'] = googletag.defineSlot('/1234/example', [[728, 90], 'gpt-divId1').addService(googletag.pubads());
adunits['slot2'] = googletag.defineSlot('/1234/example', [[300, 250], 'gpt-divId2').addService(googletag.pubads());
adunits['slot3'] = googletag.defineSlot('/1234/example', [[300, 250], 'gpt-divId3').addService(googletag.pubads());
然后,刷新成人的代码变为:

var gptdivs = [];
$('.myContainer .gpt-holder').each(function(){
    gptdivs.push(adunits[$(this).attr('data-gpt-slot')]);
});
googletag.pubads().refresh(gptdivs);

问题是您试图错误地使用刷新方法。它将实际的ad单元实例作为参数,而不仅仅是它的名称作为字符串

请尝试以下方法:

var adunits = {};

adunits['slot1'] = googletag.defineSlot('/1234/example', [[728, 90], 'gpt-divId1').addService(googletag.pubads());
adunits['slot2'] = googletag.defineSlot('/1234/example', [[300, 250], 'gpt-divId2').addService(googletag.pubads());
adunits['slot3'] = googletag.defineSlot('/1234/example', [[300, 250], 'gpt-divId3').addService(googletag.pubads());
然后,刷新成人的代码变为:

var gptdivs = [];
$('.myContainer .gpt-holder').each(function(){
    gptdivs.push(adunits[$(this).attr('data-gpt-slot')]);
});
googletag.pubads().refresh(gptdivs);

问题是您试图错误地使用刷新方法。它将实际的ad单元实例作为参数,而不仅仅是它的名称作为字符串

请尝试以下方法:

var adunits = {};

adunits['slot1'] = googletag.defineSlot('/1234/example', [[728, 90], 'gpt-divId1').addService(googletag.pubads());
adunits['slot2'] = googletag.defineSlot('/1234/example', [[300, 250], 'gpt-divId2').addService(googletag.pubads());
adunits['slot3'] = googletag.defineSlot('/1234/example', [[300, 250], 'gpt-divId3').addService(googletag.pubads());
然后,刷新成人的代码变为:

var gptdivs = [];
$('.myContainer .gpt-holder').each(function(){
    gptdivs.push(adunits[$(this).attr('data-gpt-slot')]);
});
googletag.pubads().refresh(gptdivs);

我必须将adunits更改为global,因为我的刷新代码在一个单独的文件中,但就在钱上-谢谢。我必须将adunits更改为global,因为我的刷新代码在一个单独的文件中,但就在钱上-谢谢。我必须将adunits更改为global,因为我的刷新代码在一个单独的文件中,但是就在钱上-谢谢。我不得不把adunits改成global,因为我的刷新代码在一个单独的文件中,但是就在钱上-谢谢。