最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Randomly change uppercaselower case in html text - Stack Overflow

programmeradmin2浏览0评论

I have a HTML text element ex: an H1 tag called VIDEOS. Is there any way to use JS to randomly manipulate the capitalization of the text? So for instance on one instance it loads the text as viDEoS, on another it loads ViDeos and so on.

Each letter essentially randomly changes between uppercase & lowercase

I have a HTML text element ex: an H1 tag called VIDEOS. Is there any way to use JS to randomly manipulate the capitalization of the text? So for instance on one instance it loads the text as viDEoS, on another it loads ViDeos and so on.

Each letter essentially randomly changes between uppercase & lowercase

Share Improve this question asked Apr 24, 2017 at 10:23 user3886632user3886632 873 silver badges9 bronze badges 2
  • 1 Do you at least have the code needed to get hold of the element? What part of this are you stuck on? – James Thorpe Commented Apr 24, 2017 at 10:24
  • This might be a good start w3schools./jsref/jsref_touppercase.asp – kalsowerus Commented Apr 24, 2017 at 10:25
Add a ment  | 

2 Answers 2

Reset to default 7

Possible solution.

var elem = document.getElementById('vid');

elem.textContent = elem.textContent.split('').map((v) =>
  Math.round(Math.random()) ? v.toUpperCase() : v.toLowerCase()
).join('');
<h1 id='vid'>videos</h1>

$('.randomize').each(function() {
  var _word = $(this).html();
  var _arr = _word.split('');
  var _store = '';
  var _style = '';
  
  $(this).html('');
  
  for (var i = 0, len = _arr.length; i < len; i++) {
    if((Math.floor(Math.random() * 2) + 1) === 1) {
      _style = 'uppercase';
    }
    else {
      _style = 'lowercase';
    }
    
    _store = _store + '<span style="text-transform: '+ _style +' ;">' + _arr[i] + '</span>';
  }
  
  $(this).html(_store);
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<h1 class="randomize">Videos</h1>

发布评论

评论列表(0)

  1. 暂无评论