如何在年龄验证弹出窗口中使用JavaScript和是/否按钮?

3
我不太熟悉JavaScript并正在尝试为Square Space网站设置年龄验证弹出窗口。我在Yes/No按钮功能上遇到了困难,无法弄清如何使它们正常工作。想法是当单击“Yes”时,弹出窗口消失,并且该选择将在该会话的剩余时间内保留。当单击“No”时,网站退出。非常感谢您的帮助。
我已将以下代码粘贴到整个站点的头部代码注入中:

jQuery(document).ready(function($) {

  if (sessionStorage.getItem('advertOnce') !== 'true') {
    //sessionStorage.setItem('advertOnce','true');
    $('.box').show();
  } else {
    $('.box').hide();
  };

  $('#btn-alpha').click(function() {
    sessionStorage.setItem('advertOnce', 'true');
    $('.box').hide();
  });

  $('#btn-beta').click(function() {

    window.location.href = 'http://google.com/';

  });
});
/*----------------------------------------------   
-Defualt to border-box
-----------------------------------------------  */

*,
*:before,
*:after {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}

html,
body {
  font-family: helvetica;
  font-size: 100%;
  margin: 0;
  padding: 0;
  font-weight: 400;
  font-family: Raleway;
  line-height: 1.4;
}


/*----------------------------------------------   
--Fluid body sizing
-----------------------------------------------  */

body {
  font-size: 100%;
}

@media (min-width: 32em) {
  body {
    font-size: 110%;
  }
}

@media (min-width: 54em) {
  body {
    font-size: 111%;
  }
}

@media (min-width: 74em) {
  body {
    font-size: 115%;
  }
}

@media (min-width: 96em) {
  body {
    font-size: 135%;
  }
}

a.btn {
  background-color: #e3c827;
  color: #000;
  text-decoration: none;
  display: inline-block;
  letter-spacing: 0.1em;
  padding: 0.5em 0em;
}

a.btn.btn-beta {
  background-color: #800000;
}

.overlay-verify {
  background: #000;
  position: fixed;
  height: 100%;
  width: 100%;
  top: 0;
  left: 0;
  z-index: 1;
}

.box {
  background: #fff;
  position: relative;
  left: 0;
  right: 0;
  top: 20%;
  bottom: 0;
  margin: 0 auto;
  z-index: 9;
  width: 70%;
  height: 40%;
  display: table;
}

.box .box-left,
.box .box-right {
  width: 100%;
  position: relative;
  text-align: center;
  padding: 5%;
}

@media (min-width: 54em) {
  .box .box-left,
  .box .box-right {
    display: table-cell;
    vertical-align: middle;
    width: 50%;
  }
}

.box .box-left p,
.box .box-right p {
  position: relative;
  z-index: 3;
}

.box .box-left {
  background: url(https://www.distillery.news/wp-content/uploads/2018/03/84743_Hochatown-2.jpg) 50% 50%;
  background-size: cover;
}

.box .box-left img {
  position: relative;
  z-index: 1;
  width: 9em;
}

.box .box-left:after {
  content: '';
  position: relative;
  z-index: 0;
  top: 0;
  left: 0;
  height: 100%;
  width: 100%;
  background-color: rgba(0, 0, 0, 0);
}

.box .box-right {
  background-color: #000000;
  text-align: center;
}

.box .box-right h3 {
  color: #fff;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  border-bottom: 1px solid #eee;
  padding-bottom: 1em;
  margin: 0 auto;
}

.box .box-right p {
  color: #fff;
}

.box .box-right small {
  color: #fff;
}

.box .box-right .btn {
  font-weight: 600;
  letter-spacing: 0.2em;
  padding: 0.9em 1em 0.7em;
  margin: 1em auto;
  display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<main>
  <article class="box">
    <div class="box-left">
    </div>
    <div class="box-right">
      <h3>Age Verification</h3>
      <p>This site requires you to be 21 years or older to enter. </p>
      <p>By clicking 'YES', I certify that I am 21 years or older.</p>

      <a href="#" class="btn btn-alpha" id="refresh-page">YES</a>

      <a href="#" class="btn btn-beta" id="refresh-page">NO</a>
    </div>
  </article>

  <div class="overlay-verify"></div>
</main>


1
请问Java与这个问题有什么关联吗?我不太明白。 - Snow
3
两个按钮有相同的ID显然没有什么帮助。 - Robby Cornelissen
其实,为什么需要记住“Yes”呢?你只需要一个“Yes”和“No”的按钮。点击“no”后,运行退出网站的代码。如果点击“Yes”,那么只需将它们带到您的首页或其他页面,作为由<a href="test.html">创建的纯链接即可。您需要记住Yes被点击的原因吗? - Gosi
1
@Gosi,我的担忧是每当用户返回主页时,年龄验证门户又会弹出。 - smspear32
我将你的代码添加到了一个片段中,但修复了一些明显的错别字/错误(这些错误并没有影响问题或使你的代码可运行)。我在你的jQuery上关闭了括号/圆括号,并从你的HTML中删除了一个额外的闭合</div> - disinfor
显示剩余3条评论
3个回答

3
你的 JavaScript 中将按钮类作为 ID 进行目标定位。请在以下这两行中,将 # 替换为 .
$('#btn-alpha').click(function() {    // $('.btn-alpha')

并且

$('#btn-beta').click(function() {     // $('.btn-beta')

现在应该可以正常工作了,尽管评论中提到的两个按钮也具有相同的id。Id需要是唯一的,因此页面上最多只能有一个元素具有特定的id。

以下是已更正的代码(我已注释掉了sessionStorage部分):

jQuery(document).ready(function($) {
  /*
    if (sessionStorage.getItem('advertOnce') !== 'true') {
      //sessionStorage.setItem('advertOnce','true');

    } else {
      $('.box').hide();
    };
  */
  
  $('.box').show();
  
  $('.btn-alpha').click(function() {
    //sessionStorage.setItem('advertOnce', 'true');
    $('.box').hide();
  });

  $('.btn-beta').click(function() {

    window.location.href = 'http://google.com/';

  });
});
/*----------------------------------------------   
-Defualt to border-box
-----------------------------------------------  */

*,
*:before,
*:after {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}

html,
body {
  font-family: helvetica;
  font-size: 100%;
  margin: 0;
  padding: 0;
  font-weight: 400;
  font-family: Raleway;
  line-height: 1.4;
}


/*----------------------------------------------   
--Fluid body sizing
-----------------------------------------------  */

body {
  font-size: 100%;
}

@media (min-width: 32em) {
  body {
    font-size: 110%;
  }
}

@media (min-width: 54em) {
  body {
    font-size: 111%;
  }
}

@media (min-width: 74em) {
  body {
    font-size: 115%;
  }
}

@media (min-width: 96em) {
  body {
    font-size: 135%;
  }
}

a.btn {
  background-color: #e3c827;
  color: #000;
  text-decoration: none;
  display: inline-block;
  letter-spacing: 0.1em;
  padding: 0.5em 0em;
}

a.btn.btn-beta {
  background-color: #800000;
}

.overlay-verify {
  background: #000;
  position: fixed;
  height: 100%;
  width: 100%;
  top: 0;
  left: 0;
  z-index: 1;
}

.box {
  background: #fff;
  position: relative;
  left: 0;
  right: 0;
  top: 20%;
  bottom: 0;
  margin: 0 auto;
  z-index: 9;
  width: 70%;
  height: 40%;
  display: table;
}

.box .box-left,
.box .box-right {
  width: 100%;
  position: relative;
  text-align: center;
  padding: 5%;
}

@media (min-width: 54em) {
  .box .box-left,
  .box .box-right {
    display: table-cell;
    vertical-align: middle;
    width: 50%;
  }
}

.box .box-left p,
.box .box-right p {
  position: relative;
  z-index: 3;
}

.box .box-left {
  background: url(https://www.distillery.news/wp-content/uploads/2018/03/84743_Hochatown-2.jpg) 50% 50%;
  background-size: cover;
}

.box .box-left img {
  position: relative;
  z-index: 1;
  width: 9em;
}

.box .box-left:after {
  content: '';
  position: relative;
  z-index: 0;
  top: 0;
  left: 0;
  height: 100%;
  width: 100%;
  background-color: rgba(0, 0, 0, 0);
}

.box .box-right {
  background-color: #000000;
  text-align: center;
}

.box .box-right h3 {
  color: #fff;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  border-bottom: 1px solid #eee;
  padding-bottom: 1em;
  margin: 0 auto;
}

.box .box-right p {
  color: #fff;
}

.box .box-right small {
  color: #fff;
}

.box .box-right .btn {
  font-weight: 600;
  letter-spacing: 0.2em;
  padding: 0.9em 1em 0.7em;
  margin: 1em auto;
  display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<main>
  <article class="box">
    <div class="box-left">
    </div>
    <div class="box-right">
      <h3>Age Verification</h3>
      <p>This site requires you to be 21 years or older to enter. </p>
      <p>By clicking 'YES', I certify that I am 21 years or older.</p>

      <a href="#" class="btn btn-alpha" id="refresh-page-yes">YES</a>

      <a href="#" class="btn btn-beta" id="refresh-page-no">NO</a>
    </div>
  </article>

  <div class="overlay-verify"></div>
</main>


@smspear32 这里无法使用location.href,因为“运行代码片段”在一个iframe中。 - Ivan86

2
这里有一个可行的示例(堆栈片段可能无法保存sessionStorage,因此请在此处检查:https://jsfiddle.net/v358z1yt/

jQuery(document).ready(function($) {

  if (sessionStorage.getItem('advertOnce') !== 'true') {
    $('.box').show();
  } else {
    $('.box').hide();
  };

  $('.btn-alpha').click(function() {
    sessionStorage.setItem('advertOnce', 'true');
    $('.box').hide();
  });

  $('.btn-beta').click(function() {
    window.location.href = 'http://google.com/';

  });
});
/*----------------------------------------------   
-Defualt to border-box
-----------------------------------------------  */

*,
*:before,
*:after {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}

html,
body {
  font-family: helvetica;
  font-size: 100%;
  margin: 0;
  padding: 0;
  font-weight: 400;
  font-family: Raleway;
  line-height: 1.4;
}


/*----------------------------------------------   
--Fluid body sizing
-----------------------------------------------  */

body {
  font-size: 100%;
}

@media (min-width: 32em) {
  body {
    font-size: 110%;
  }
}

@media (min-width: 54em) {
  body {
    font-size: 111%;
  }
}

@media (min-width: 74em) {
  body {
    font-size: 115%;
  }
}

@media (min-width: 96em) {
  body {
    font-size: 135%;
  }
}

a.btn {
  background-color: #e3c827;
  color: #000;
  text-decoration: none;
  display: inline-block;
  letter-spacing: 0.1em;
  padding: 0.5em 0em;
}

a.btn.btn-beta {
  background-color: #800000;
}

.overlay-verify {
  background: #000;
  position: fixed;
  height: 100%;
  width: 100%;
  top: 0;
  left: 0;
  z-index: 1;
}

.box {
  background: #fff;
  position: relative;
  left: 0;
  right: 0;
  top: 20%;
  bottom: 0;
  margin: 0 auto;
  z-index: 9;
  width: 70%;
  height: 40%;
  display: table;
}

.box .box-left,
.box .box-right {
  width: 100%;
  position: relative;
  text-align: center;
  padding: 5%;
}

@media (min-width: 54em) {
  .box .box-left,
  .box .box-right {
    display: table-cell;
    vertical-align: middle;
    width: 50%;
  }
}

.box .box-left p,
.box .box-right p {
  position: relative;
  z-index: 3;
}

.box .box-left {
  background: url(https://www.distillery.news/wp-content/uploads/2018/03/84743_Hochatown-2.jpg) 50% 50%;
  background-size: cover;
}

.box .box-left img {
  position: relative;
  z-index: 1;
  width: 9em;
}

.box .box-left:after {
  content: '';
  position: relative;
  z-index: 0;
  top: 0;
  left: 0;
  height: 100%;
  width: 100%;
  background-color: rgba(0, 0, 0, 0);
}

.box .box-right {
  background-color: #000000;
  text-align: center;
}

.box .box-right h3 {
  color: #fff;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  border-bottom: 1px solid #eee;
  padding-bottom: 1em;
  margin: 0 auto;
}

.box .box-right p {
  color: #fff;
}

.box .box-right small {
  color: #fff;
}

.box .box-right .btn {
  font-weight: 600;
  letter-spacing: 0.2em;
  padding: 0.9em 1em 0.7em;
  margin: 1em auto;
  display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<main>
  <article class="box">
    <div class="box-left">
    </div>
    <div class="box-right">
      <h3>Age Verification</h3>
      <p>This site requires you to be 21 years or older to enter. </p>
      <p>By clicking 'YES', I certify that I am 21 years or older.</p>

      <a href="#" class="btn btn-alpha">YES</a>

      <a href="#" class="btn btn-beta">NO</a>
    </div>
  </article>

  <div class="overlay-verify"></div>
</main>

问题在于你使用了 #btn-alpha#btn-beta 作为选择器,但是你需要使用类名。

另外,我从你的按钮中删除了重复的id。这是不可以的。


0
谢谢大家的评论!我忘记匹配ID了,我的疏忽。现在用'.'而不是'#'把它们当作类处理似乎已经修复了“YES”按钮,但第二个按钮仍然无法正常工作。从另一个问题(window.location.href not working)的进一步研究使我添加了以下内容。
return false;

在 window.location.href 之后,但仍然不起作用。有什么想法吗?

jQuery(document).ready(function($) {

  if (sessionStorage.getItem('advertOnce') !== 'true') {
    $('.box').show();
  } else {
    $('.box').hide();
  };

  $('.btn-alpha').click(function() {
    sessionStorage.setItem('advertOnce', 'true');
    $('.box').hide();
  });

  $('.btn-beta').click(function() {
    window.location.href = "https://www.google.com/";
return false;
  });
});

第二个按钮在StackOverflow上不起作用,但在您的环境中应该正常工作,因为脚本在我们无法控制的iframe内运行。这是当我从我的答案中运行代码时出现在控制台中的错误消息:Refused to display 'https://www.google.com/' in a frame because it set 'X-Frame-Options' to 'sameorigin'. - Ivan86
因此,通过在控制台中看到错误,这意味着它已经起作用,并且浏览器尝试切换位置,但由于iframe设置而无法实现。 - Ivan86

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接