剧透 - javascript + html

时间:2016-05-22 11:02:07

标签: javascript html

我需要一点帮助...... 我得到了#34; some.js"与经典:

function somequotes() {
  var quotes = new Array()

  quotes[0]='some text'
  quotes[1]='some more text'
  //...
  //...

  var whichquote = Math.floor(Math.random()*(quotes.length));

  document.write(quotes[whichquote])}
}

somequotes();

和" some.html":

<script language="JavaScript" src="some.js"></script>

关于笑话,我需要隐藏一些引号(比如剧透),因为有些人是18岁......

所以如果:

quotes[4] = 'some 18+ text'

什么是隐藏部分报价的最佳脚本?

1 个答案:

答案 0 :(得分:0)

使用单个列表

如果您有违规报价清单:

function somequotes() {
  var quotes = new Array()

  quotes[0]='some text'
  quotes[1]='some more text'
  //...

  quotes[2]='some 18+ text'

  // Here you specify the list of offensive questions
  offensive = [2];

  var whichquote = Math.floor(Math.random()*(quotes.length));
  var quote = quotes[whichquote];

  if (offensive.indexOf(whichquote) == -1)
    document.write(quotes[whichquote]);
  else
    document.write('<p id="quote-wrap"><button id="quote" data-text="' + quote + '">Reveal</button></p>');
}

function revealer() {
  var quoteWrap = document.getElementById("quote-wrap");
  var quote = document.getElementById("quote");
  if (quoteWrap) {
    quote.addEventListener("click", function( event ) {
      quoteWrap.innerHTML = quote.getAttribute('data-text');
    }, false);
  }
}

somequotes();
revealer();

分隔列表

您也可以将列表分成两部分:

quotes_normal = [
    'some text',
    'some more text'
];

quotes_nsfw = [
    'some 18+ text'
];

然后根据用户是否可以查看18个以上的引号生成一个列表:

if (is_adult)
    var quotes = quotes_normal.concat(quotes_nsfw);
else
    var quotes = quotes_normal;

var whichquote = Math.floor(Math.random() * quotes.length);
var quotetext = quotes[whichquote];