【问题标题】:Remove a div after is clicked [closed]单击后删除 div [关闭]
【发布时间】:2016-05-11 20:23:06
【问题描述】:

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

这是我的代码:

  $("#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>

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

【问题讨论】:

  • 可以发html吗?
  • 哪一行报错?你可以从你的开发者控制台检查它。顺便说一句,如果你使用 jQuery,你可以通过 $.cookie('clicked', 'true') 设置 cookie。
  • “它不起作用”不是任何人都可以帮助您的。 什么不起作用?
  • @JürgenKruja:工作正常:jsfiddle.net/3pqjb24x 这对您来说究竟是如何失败的?您必须描述问题,这里没有人可以看到您的屏幕。
  • “它不工作”......它整天躺在沙发上看电视吗? :-)

标签: javascript jquery cookies setcookie


【解决方案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();
  });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-19
    • 1970-01-01
    • 2014-12-27
    • 2021-02-18
    • 1970-01-01
    • 1970-01-01
    • 2016-12-14
    相关资源
    最近更新 更多