【问题标题】:Paragraph random UPPERCASE words after mouseenter <p>mouseenter <p> 之后的段落随机大写单词
【发布时间】:2018-06-30 15:22:10
【问题描述】:

我有 3 个段落。在P 标签上使用鼠标悬停事件后,我需要 JS 代码使这个虚拟文本中的随机单词变为大写。它会在我悬停的每个段落上每 3 秒随机更改一次。

HTML:

<head>
    <style>
     p {
         margin-top: 50px;
         font-size: 24px;
     }
    </style>
</head>
<body>
  <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. </p>
  <p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old</p>
  <p>"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</body>

【问题讨论】:

  • 你试过了吗?
  • 我们不是来做你的(家庭)工作的。请添加您在研究过程中发现的相关信息、到目前为止您尝试过的内容以及您在尝试中遇到的问题(最好是minimal reproducible example)。
  • 问题是我在考试中遇到了这个问题。我试着做点什么,但我不知道要开始。
  • 老实说我不知道​​该怎么做

标签: javascript html settimeout uppercase mouseenter


【解决方案1】:

[...document.getElementsByTagName('p')].forEach(elem => {
  let oldText = elem.innerHTML;
  elem.onmouseenter = () => {
    elem.onmouseenter = '';
    setInterval(() => elem.innerHTML = oldText.split('').map(word => Math.random() > 0.15 ? word.toLowerCase() : word.toUpperCase()).join(''), 3000);
  }
})
<head>
  <style>
    p {
      margin-top: 50px;
      font-size: 24px;
    }
  </style>
</head>

<body>
  <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. </p>
  <p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old</p>
  <p>"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</body>

【讨论】:

  • 感谢侮辱和回答
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-09
  • 2014-07-09
  • 2011-02-11
  • 2015-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多