随机更改鼠标上的文本/out on nout



我有非常简单但令人沮丧的问题。基本上,我要做的是鼠标&在特定元素中,元素文本将通过数组随机变化。这是我的代码,从HTML开始:

<div class="logo">
    <a href="#">
        luke <span>whitehouse</span> 
    </a>
    <span class="logo-note">// front-end web designer</span>
</div>

和Heres JS:

$(document).ready(function() {
    var quotes = new Array("foo", "bar", "baz", "chuck");
    var randno = Math.floor ( Math.random() * quotes.length );
    $('.quote').add(quotes[randno]);
    $('.logo a').mouseover(function() {
        $('.logo-note').text(quotes[randno]);
    }).mouseout(function() {
        $('.logo-note').text(quotes[randno]);
    });
});

当鼠标上/输出事件发生时,您需要获取随机文本。在您的代码中,randno是一次计算一次,并且永远不会更改,因此您每次都会不断获得相同的数组元素。尝试以此为主意

$('.logo a').mouseover(function() {
        $('.logo-note').text(quotes[Math.floor ( Math.random() * quotes.length )]);
    }).mouseout(function() {
        $('.logo-note').text(quotes[Math.floor ( Math.random() * quotes.length )]);
    });

最新更新