MouseOver和On MouseLeave上的随机文本显示真实文本

我想要一个javascript函数,如果我将鼠标hover在像“ExampleText”这样的文本上,它将生成一些具有相同长度的hover文本的随机字符,如“$ 45a%b8r5c7”,并且每次生成随机字符。如果我在文本上取消hover它会显示旧的orignal文本,即“ExampleText”。

如果你想指定生成的字符,也许你可以使用这样的东西:

https://jsfiddle.net/uk7xyapa/1/

HTML:

ExampleText ExampleText 

jQuery的:

 $(document).ready(function(){ var originalText = $( "#originalText" ).text(); $( "#originalText" ).mouseenter(function() { var text = ''; var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789%$'; for(var i=0; i < $( "#originalText" ).text().length; i++) { text += possible.charAt(Math.floor(Math.random() * possible.length)); } $( "#newText" ).text( text ); }); $( "#originalText" ).mouseleave(function() { $( "#newText" ).text( originalText ); }); }) 

试试这个

 $('body').on('mouseenter', '.change', function(){ $('.change').html(Math.random().toString(36).substring(7)); }).on('mouseleave', function(){ $('.change').html('Example'); }); 
Example