2016-05-11 80 views
-2

我想在点击后删除div的父级,并将值“true”的cookie“存储”。我使用JavaScript Cookie来存储Cookie。所以我有一个AdSense的博客,我想隐藏我的广告,以防止无效活动后用户点击它1次。点击后删除div

这是我的代码:

$("#firstCode").click(function() { 
     $(this).parent().hide(); 

     var date = new Date(); 
     var h = "1"; 
     date.setTime(date.getTime() + (h * 60 * 60 * 1000)); 

     Cookies.set('clicked', 'true', {expires: date, path: '/'}); 
    }); 

和HTML代码:

<div id="codeWrapper"> 
    <div id="firstCode"> 
     <script type="text/javascript"> 
     google_ad_client = "ca-pub-7094677798399606"; 
     google_ad_slot = "8373705259"; 
     google_ad_width = 728; 
     google_ad_height = 90; 
     </script> 
     <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
     </script> 
</div> 

,但它不工作。如果有人能找到我的问题在哪里,我非常感谢。

+0

可以置HTML中删除元素? –

+0

哪一行会引发错误?你可以从开发者控制台检查它。顺便说一句,如果你使用jQuery,你可以通过$ .cookie('clicked','true')'来设置cookie。 – choz

+0

当你说**但它不起作用**,你的意思是隐藏div或cookes存储? –

回答

1

您可以使用jQuery remove从DOM

$("#firstCode").click(function() { 
    var date = new Date(); 
    var h = "<?php echo html_entity_decode(get_option('fs_time_cookie')); ?>"; 
     date.setTime(date.getTime() + (h * 60 * 60 * 1000)); 
     Cookies.set('clicked', 'true', {expires: date, path: '/'}); 
    $(this).parent().remove(); 
    });