先看效果如下

目录如下

//index.html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width">
  <title>笑话生成器</title>
  <link rel="stylesheet" href="style.css">
  <script src="ui.js" defer></script>
  <script src="main.js" defer></script>
</head>

<body>
  <div>
    <label for="customname" id="lbl-customname">请输入自定义的名字:</label>
    <input id="customname" type="text" placeholder="李雷">
  </div>
  <div>
    <label for="cn" id="lbl-cn">中国</label>
    <input id="cn" type="radio" name="country" value="cn" checked>
    <label for="us" id="lbl-us">美国</label>
    <input id="us" type="radio" name="country" value="us">
    <label for="uk" id="lbl-uk">英国</label>
    <input id="uk" type="radio" name="country" value="uk">
  </div>
  <div>
    <button class="randomize">随机生成笑话</button>
  </div>
  <p class="story"></p>
</body>
</html>
//main.js
const customName = document.getElementById('customname');
const randomize = document.querySelector('.randomize');
const story = document.querySelector('.story');

function randomValueFromArray(array){
  const random = Math.floor(Math.random()*array.length);
  return array[random];
}

const storyText = 'It was 94 fahrenheit outside, so :insertx: went for a walk. When they got to :inserty:, they stared in horror for a few moments, then :insertz:. Bob saw the whole thing, but was not surprised — :insertx: weighs 300 pounds, and it was a hot day.';
const insertX = ['Willy the Goblin','Big Daddy','Father Christmas'];
const insertY = ['the soup kitchen','Disneyland','the White House'];
const insertZ = ['spontaneously combusted','melted into a puddle on the sidewalk','turned into a slug and crawled away'];

const storyTextZh = '外边有34度,于是:inserta:出去遛弯。当走到:insertb:时他们被眼前的景象惊呆了,然后就:insertc:。李雷全程目睹但他并没有慌,因为:inserta:是一个270斤的胖子,天气又辣么热。';
const insertA = ['怪兽威利', '大老爹', '圣诞老人'];
const insertB = ['救助站', '迪士尼乐园', '白宫'];
const insertC = ['自燃了', '在人行道化成了一坨泥', '变成一条鼻涕虫爬走了'];

randomize.addEventListener('click', result);

function result() {
  let newStory, xItem, yItem, zItem, name;
  if (document.getElementById('cn').checked) {
    newStory = storyTextZh;

    xItem = randomValueFromArray(insertA);
    yItem = randomValueFromArray(insertB);
    zItem = randomValueFromArray(insertC);

    newStory = newStory.replace(':inserta:', xItem);
    newStory = newStory.replace(':inserta:', xItem);
    newStory = newStory.replace(':insertb:', yItem);
    newStory = newStory.replace(':insertc:', zItem);

    if (customName.value !== '') {
      name = customName.value;
      newStory = newStory.replace('李雷', name);
    }
  } else {
    newStory = storyText;

    xItem = randomValueFromArray(insertX);
    yItem = randomValueFromArray(insertY);
    zItem = randomValueFromArray(insertZ);

    newStory = newStory.replace(':insertx:', xItem);
    newStory = newStory.replace(':insertx:', xItem);
    newStory = newStory.replace(':inserty:', yItem);
    newStory = newStory.replace(':insertz:', zItem);

    if (customName.value !== '') {
      name = customName.value;
      newStory = newStory.replace('Bob', name);
    }

    if (document.getElementById("uk").checked) {
      const weight = Math.round(300 * 0.0714286) + ' stone';
      const temperature = Math.round((94 - 32) * 5 / 9) + ' centigrade';
      newStory = newStory.replace('94 fahrenheit', temperature);
      newStory = newStory.replace('300 pounds', weight);
    }
  }

  story.textContent = newStory;
  story.style.visibility = 'visible';
}

//style.css
body {
    font-family: helvetica, sans-serif;
    width: 350px;
    border: 1px solid;
    padding: 1em;
}

label {
  font-weight: bold;
}

div {
  padding-bottom: 20px;
}

input[type="text"] {
  padding: 5px;
  width: 150px;
}

p {
  background: #FFC125;
  color: #5E2612;
  padding: 10px;
  visibility: hidden;
}
//ui.js
document.getElementById('cn').onclick = () => {
    document.title = '笑话生成器';
    document.getElementById('lbl-customname').textContent = '请输入自定义的名字:';
    document.getElementById('lbl-cn').textContent = '中国';
    document.getElementById('lbl-us').textContent = '美国';
    document.getElementById('lbl-uk').textContent = '英国';
    document.getElementById('customname').placeholder = '李雷';
    document.querySelector('.randomize').textContent = '随机生成笑话';
  };

  document.getElementById('us').onclick =
  document.getElementById('uk').onclick = () => {
    document.title = 'Silly story generator';
    document.getElementById('lbl-customname').textContent = 'Enter custom name:';
    document.getElementById('lbl-cn').textContent = 'CN';
    document.getElementById('lbl-us').textContent = 'US';
    document.getElementById('lbl-uk').textContent = 'UK';
    document.getElementById('customname').placeholder = 'Bob';
    document.querySelector('.randomize').textContent = 'Generate random story';
  };

jokes的更多相关文章

  1. Programmer's Jokes

    1.   一天, 下着小雨, 和同事们一起去上班,一位同事差点滑倒,另一位同事笑话说:如果人走路用爬的就不会摔倒了!   遂反问他:能用两行代码搞定的事情为什么要用4行呢? 2.  有趣的公式( Fr ...

  2. python_爬虫一之爬取糗事百科上的段子

    目标 抓取糗事百科上的段子 实现每按一次回车显示一个段子 输入想要看的页数,按 'Q' 或者 'q' 退出 实现思路 目标网址:糗事百科 使用requests抓取页面  requests官方教程 使用 ...

  3. words

    conscious[英][ˈkɒnʃəs][美][ˈkɑnʃəs]consensus[英][kənˈsensəs][美][kənˈsɛnsəs] scious sensuswaterflood; de ...

  4. http-code 未译

    1xx Informational Request received, continuing process. This class of status code indicates a provis ...

  5. 【转】去掉Sqlite3 数据库中的前后回车换行符(newline)

    原文: http://www.blogjava.net/pts/archive/2013/06/10/400... 时间: 2013-06-10 转自:http://www.ityuedu.com/a ...

  6. MongoDB Shard部署及Tag的使用

    Shard部署 准备测试环境 为准备数据文件夹 Cd  /home/tiansign/fanr/mongodb/Shard mkdir configdb1 configdb2 configdb3 mk ...

  7. android 资讯阅读器

    最近找申请到了一个不错的接口 , 非常适合拿来写一个资讯类的app. 现在着手写,随写随更.也算是抛砖引玉.烂尾请勿喷.╭(╯^╰)╮ android 资讯阅读器 第一阶段目标样式(滑动切换标签 , ...

  8. [zz] Pixar’s OpenSubdiv V2: A detailed look

    http://www.fxguide.com/featured/pixars-opensubdiv-v2-a-detailed-look/ Pixar’s OpenSubdiv V2: A detai ...

  9. [计算机取证技术] VDI-in-a-Box Analysis Results

    原文跳转: http://dig4n6.blogspot.tw/2013/07/vdi-in-box-analysis-results.html *文中引用图片如无法浏览,请科学上网* VDI-in- ...

随机推荐

  1. 【apache2】AH00543: httpd: bad user name apache

    当启动 apache 时,出现一下异常:AH00543: httpd: bad user name daemon 解决方法:            #groupadd daemon          ...

  2. QTP自动化测试-按行取值(win10下输入?问题)-笔记20181119

    在win10下运行qtp10 所有输入汉字都会为?,在win7下可以.查询了百度.bingo没有解决问题.当前的解决办法 ,在脚本中使用DataTable取数据值,添加2行记录,一行使用汉字,一行使用 ...

  3. vue中组件绑定事件时是否加.native

    组件绑定事件时 1. 普通组件绑定事件不能添加.native, 添加后事件失效 2. 自定义组件绑定事件需要添加.native, 否则事件无效 <template> <!-- < ...

  4. 梯度下降取负梯度的简单证明,挺有意思的mark一下

    本文转载自:http://blog.csdn.net/itplus/article/details/9337515

  5. Zero to Build: Create new Xamarin apps in minutes with AppMap

    Creating a new Xamarin.Forms app can be an intimidating task, especially if you add in content pages ...

  6. Nintex Workflow Get Attachment link

    不多解释,直接上图,操作简单

  7. MailUtils类:用于发送激活邮件

    该类用于发送激活邮件 package com.itheima.utils; import java.util.Properties; import javax.mail.Authenticator; ...

  8. Js 布尔值操作符

    在js中,逻辑与(&&) 和 逻辑或(||)可以对任意的数据类型进行操作,而在高级程序设计中只给出了一系列的规则,并没有进行解释,所以经常记不住.在读其它书籍的时候,读到了它的原理,其 ...

  9. luogu T40984Chino的成绩

    Chino的成绩 题目描述 Chino非常注重自己的成绩 Chino有m种方式给自己增加rp以增加成绩,她的每种增加rp的方式都有n个阶段,第i种的第j个阶段增加的rp表示为Aij​,表示连续进行了j ...

  10. Centos安装python3

    安装环境 系统:阿里云服务器centos7.5系统 看见好多博客对centos安装python3的方式各不相同且都不完整,今天我来完整的演示安装python3 1.下载python3源码包 命令 wg ...