0

我有这段代码可以根据用户是否单击按钮来隐藏/显示 div。它还设置了一个 cookie 来记住状态,因此如果用户刷新页面,div 状态保持不变。一切正常,直到我添加代码以显示 DFP 广告,并且由于某种原因,它使用的 iframe 在刷新页面时覆盖了隐藏状态 - 它仍然显示广告,即使包含 div 被隐藏?

这是我的代码,我将不胜感激任何帮助进行排序:-)

谢谢

现在是http://jsfiddle.net/jamesisapayne/xhzes/

<style>
#billboardButton {
    background-color:#f1f1f1;
    color:#666;
    padding:3px;
    width:100px;
    cursor:pointer;
    text-align:center;
    margin:0 auto;
}
</style>

<!-- Load the latest version of jQuery -->
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<!-- Load the jquery.cookie.js plugin -->
<script src="https://googledrive.com/host/0B7ZDS1Dob8_8WXhpUVdtNjVNdWc/"></script>

<script type='text/javascript'>
var googletag = googletag || {};
googletag.cmd = googletag.cmd || [];
(function() {
    var gads = document.createElement('script');
    gads.async = true;
    gads.type = 'text/javascript';
    var useSSL = 'https:' == document.location.protocol;
    gads.src = (useSSL ? 'https:' : 'http:') + 
    '//www.googletagservices.com/tag/js/gpt.js';
    var node = document.getElementsByTagName('script')[0];
    node.parentNode.insertBefore(gads, node);
})();
</script>
<script type='text/javascript'>
googletag.cmd.push(function() {
    googletag.defineSlot('/4856165/Forum_Billboard_970x250', [970, 250], 'billboard').addService(googletag.pubads());
    googletag.pubads().setTargeting('SiteType', 'giraffetest');
    googletag.enableServices();
});
</script>

<script>
$(document).ready(function(){
    // Check to see if the billboardStatus cookie has been set
    if($.cookie('billboardStatus')) {
        // If yes, define the variable
        var cook = $.cookie('billboardStatus');
    } else {
        // Else set it as true by default and with an expiry date of 1 day = 24 hours
        var cook = $.cookie('billboardStatus', 'true', {expires: 1});
    }
    // On refresh, if the cookie value is false i.e. the ad spot is closed
    if(cook=='false') {
        // hide the billboard ad spot       
        $('#billboard').hide();
        // Change the open/close button settings
        $("#billboardButton").css("backgroundColor", "#e1e1e1").text('Open Ad');        
    } else { // Else if the value is set as true i.e. open
    // Show the ad spot
    $('#billboard').show();
        // Change the open/close button settings
        $("#billboardButton").text('Close Ad');
    }
    // When the user clciks on the open/close button…
    $('#billboardButton').on('click', function() {
        // Toggle the opening/closing of the ad spot
        $('#billboard').stop().slideToggle('slow', function(){
            // Set the background colour and the text value of the button depending on what state the button is currently in
            $("#billboardButton").css("backgroundColor", $(this).is(':not(:visible)') ? "#e1e1e1" : "").text($(this).is(':visible') ? 'Close Ad' : 'Open Ad');
            // Set the cookie value to false i.e. closed
            $.cookie('billboardStatus', $(this).is(':visible'), {expires:1});
        }); // End slideToggle
    }); // End on click
}); // End document ready
</script>
<div style="width:970px;margin:20px auto;">
    <div id="billboardButton">Close Ad</div>
    <div id='billboard' style='width:970px; height:250px; background-color:#0C9;'>
        <script type='text/javascript'>
            //googletag.cmd.push(function() { googletag.display('billboard'); });
        </script>
    </div>
</div>
4

2 回答 2

-1

通过 jQuery 添加 dfp 代码而不是直接添加到 html 中,设法找到了答案。已将调用代码放入两行(59 和 73)并对其进行了测试。这是完成的代码……</p>

<style>
#billboardButton {
    background-color:#f1f1f1;
    color:#666;
    padding:3px;
    width:100px;
    cursor:pointer;
    text-align:center;
    margin:0 auto;
}
</style>

<!-- Load the latest version of jQuery -->
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<!-- Load the jquery.cookie.js plugin -->
<script src="https://googledrive.com/host/0B7ZDS1Dob8_8WXhpUVdtNjVNdWc/"></script>

<script type='text/javascript'>
var googletag = googletag || {};
googletag.cmd = googletag.cmd || [];
(function() {
    var gads = document.createElement('script');
    gads.async = true;
    gads.type = 'text/javascript';
    var useSSL = 'https:' == document.location.protocol;
    gads.src = (useSSL ? 'https:' : 'http:') + 
    '//www.googletagservices.com/tag/js/gpt.js';
    var node = document.getElementsByTagName('script')[0];
    node.parentNode.insertBefore(gads, node);
})();
</script>
<script type='text/javascript'>
googletag.cmd.push(function() {
    googletag.defineSlot('/4856165/Forum_Billboard_970x250', [970, 250], 'billboard').addService(googletag.pubads());
    googletag.pubads().setTargeting('SiteType', 'giraffetest');
    googletag.enableServices();
});
</script>

<script>
$(document).ready(function(){
    // Check to see if the billboardStatus cookie has been set
    if($.cookie('billboardStatus')) {
        // If yes, define the variable
        var cook = $.cookie('billboardStatus');
    } else {
        // Else set it as true by default and with an expiry date of 1 day = 24 hours
        var cook = $.cookie('billboardStatus', 'true', {expires: 1});
    }
    // On refresh, if the cookie value is false i.e. the ad spot is closed
    if(cook=='false') {
        // hide the billboard ad spot       
        $('#billboard').hide();
        //$('iframe,#google_ads_iframe_/4856165/Forum_Billboard_970x250_0').css('display','none');
        // Change the open/close button settings
        $("#billboardButton").css("backgroundColor", "#e1e1e1").text('Open Ad');        
    } else { // Else if the value is set as true i.e. open
    // Show the ad spot
    $('#billboard').show().html("<script type='text/javascript'>googletag.cmd.push(function() { googletag.display('billboard'); });<\/script>");
        // Change the open/close button settings
        $("#billboardButton").text('Close Ad');
    }


    // When the user clicks on the open/close button…
    $('#billboardButton').on('click', function() {
        // Toggle the opening/closing of the ad spot
        $('#billboard').stop().slideToggle('slow', function(){
            // Set the background colour and the text value of the button depending on what state the button is currently in
            $("#billboardButton").css("backgroundColor", $(this).is(':not(:visible)') ? "#e1e1e1" : "").text($(this).is(':visible') ? 'Close Ad' : 'Open Ad');
            // Set the cookie value to false i.e. closed
            $.cookie('billboardStatus', $(this).is(':visible'), {expires:1});   
            $("#billboard").is(':visible') ? $("#billboard").html("<script type='text/javascript'>googletag.cmd.push(function() { googletag.display('billboard'); });<\/script>") : "";     
        }); // End slideToggle
        return false; 
    }); // End on click
}); // End document ready
</script>
<div style="width:970px;margin:20px auto;">
    <div id="billboardButton">Close Ad</div>
    <div id='billboard' style='width:970px; height:250px; background-color:#0C9;'></div>
</div>
于 2013-07-04T13:51:38.510 回答
-1

只需使用

onClick="funtionName()"

funtion funtionName(){ 
  $('#iframeID').toggle(); 
}
于 2019-05-27T11:39:53.617 回答