简体   繁体   English

Javascript:随机文本,鼠标悬停时显示真实文本

[英]Javascript: Random text, on mouseover show real text

I'd like to have a list of all authors of a Wordpress blog to be shown as random characters, which change on mouseover to real names. 我想列出一个Wordpress博客的所有作者,以随机字符的形式显示,这些字符在鼠标悬停时变为实名。 I'm quite new to JavaScript and the code I have here is based on Random Text On MouseOver And On MouseLeave Show The Real Text , as Amir was looking for almost the same thing as me, only the other way around. 我对JavaScript很陌生,这里的代码基于MouseOver和MouseLeave上的Random Text显示The Real Text ,因为Amir一直在寻找与我几乎相同的东西,反之亦然。

I want a unique string of random characters for each name, and I know I shouldn't use the class attribute in order to do that, but how can I use id without having to list all the names manually? 我希望每个名称都有唯一的随机字符字符串,而且我知道我不应该使用class属性来执行此操作,但是如何使用id而不需要手动列出所有名称?

Secondly, the random character string isn't showing up until you first mouseover the text because I use mouseenter , but what is the proper way to do this? 其次,直到您第一次将鼠标悬停在文本上时,随机字符串才会显示,因为我使用mouseenter ,但是这样做的正确方法是什么?

Thanks in advance! 提前致谢!

Javascript : Javascript

$(document).ready(function(){
var change_this = $( ".change_this" ).text();

$( ".change_this" ).mouseenter(function() {
    $( ".change_this" ).text( change_this );
});

    $( ".change_this" ).mouseleave(function() {
    var text = '';
    var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789%$';

    for(var i=0; i < $( ".change_this" ).text().length; i++)
    {
        text += possible.charAt(Math.floor(Math.random() * possible.length));
    }
    $( ".change_this" ).text( text );
});
})

PHP (I just show everything): PHP (我只是展示了一切):

foreach ( $blogusers as $user ) {
echo '<p><a href="http://www.untitled-jpg.nl/author/' . esc_html( $user->user_login ) . '/"><span class="x">' . esc_html( $user->display_name ) . '.jpg</span></a></p>'; 
}
?>

For testing the Javascript: http://jsfiddle.net/pqkdx1sn/1/#&togetherjs=4GUpxUpfvB 用于测试Javascript: http : //jsfiddle.net/pqkdx1sn/1/#&togetherjs=4GUpxUpfvB

So yeah, first thing you want to do, as you don't want to have all your elements crashing, is realise that $().each() is your friend. 所以,是的,您不想做的所有事情都让您崩溃,所以您首先要意识到$()。each()是您的朋友。 If you have each of the elements to be blurred process themselves and handle their enter and leave events separately, you won't have the issue you do. 如果您需要自己处理每个要模糊的元素,并分别处理它们的进入和离开事件,那么您就不会遇到问题。 Take a look at this: 看看这个:

 $(document).ready(function() { $(".change_this").each(function(){ var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789%$', randomText = ""; // Now create a random string of the same length... for (var i = 0; i < $(this).text().length; i++) { randomText += possible.charAt(Math.floor(Math.random() * possible.length)); } $(this).attr('data-randomtext', randomText).attr("data-text", $(this).text() ).text(randomText); $(this).on({ mouseenter: function() { $(this).text($(this).attr("data-text")) }, mouseleave: function(){ $(this).text($(this).attr("data-randomtext")); } }) }); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <p><span class="change_this">Joe Smith</span>.jpg</p> <p><span class="change_this">Daniel23</span>.jpg</p> <p><span class="change_this">Mariahbirsak</span>.jpg</p> 

The first thing I do is have EACH of the tagged elements store both the original text and the random text as date attributes on themselves. 我要做的第一件事是让每个标记元素都将原始文本和随机文本都存储为自身的日期属性。 Then I immediately replace the original text with the random text. 然后,我立即将原始文本替换为随机文本。 Then, when this particular element gets a mouse over/leave event, it grabs its own data attribute as needed. 然后,当该特定元素发生鼠标悬停/离开事件时,它会根据需要获取自己的data属性。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM