将随机变量传递给 addClass

发布于 2024-11-17 06:15:48 字数 1580 浏览 1 评论 0原文

只是想生成一个随机数,然后将其作为类添加到现有的 div 中。

但无法让addClass识别该变量。

JS

$(function() {
    $("#button_text").mouseenter(function() {
        var randomNum = Math.Round(Math.random()*3);
        $("#buttons").addClass(randomNum);
    }).mouseleave(function() {
        $("#buttons").removeClass(randomNum)
    });
});

HTML

<div id="buttons">
</div>

非常感谢任何帮助。


感谢您的所有帮助。

最终的解决方案是......

JS

$(function() {
    $("#button_text").mouseenter(function() {
        randomNum = 'rand' + Math.round(Math.random()*2);
        $("#buttons").addClass(randomNum);
    }).mouseleave(function() {
        $("#buttons").removeClass(randomNum)
    });
});

HTML

<div id="buttons">
</div>
<p>Insert intro sentence here.</p>
<div id="button_text">
  <p>Insert text here that will display the random button background when moused over</p>
</div>

CSS

#button_text
{
    display: block;
}
#buttons
{
    width: 200px;
    height: 200px;
    display: block;
    position: absolute;
    top: 145px;
    left: 370px;
    z-index: -999;
}
#buttons.rand0
{
    background: url(/hover_buttons.png) no-repeat;
    background-position: 0 0;
}
#buttons.rand1
{
    background: url(/hover_buttons.png) no-repeat;
    background-position: 0 -230px;
}
#buttons.rand2
{
    background: url(/hover_buttons.png) no-repeat;
    background-position: 0 -440px;
}

Just want to generate a random number and then add it as a class to an existing div.

But can't get the variable to be recognised by addClass.

JS

$(function() {
    $("#button_text").mouseenter(function() {
        var randomNum = Math.Round(Math.random()*3);
        $("#buttons").addClass(randomNum);
    }).mouseleave(function() {
        $("#buttons").removeClass(randomNum)
    });
});

HTML

<div id="buttons">
</div>

Any help much appreciated.


Thanks for all the help.

Final solution was...

JS

$(function() {
    $("#button_text").mouseenter(function() {
        randomNum = 'rand' + Math.round(Math.random()*2);
        $("#buttons").addClass(randomNum);
    }).mouseleave(function() {
        $("#buttons").removeClass(randomNum)
    });
});

HTML

<div id="buttons">
</div>
<p>Insert intro sentence here.</p>
<div id="button_text">
  <p>Insert text here that will display the random button background when moused over</p>
</div>

CSS

#button_text
{
    display: block;
}
#buttons
{
    width: 200px;
    height: 200px;
    display: block;
    position: absolute;
    top: 145px;
    left: 370px;
    z-index: -999;
}
#buttons.rand0
{
    background: url(/hover_buttons.png) no-repeat;
    background-position: 0 0;
}
#buttons.rand1
{
    background: url(/hover_buttons.png) no-repeat;
    background-position: 0 -230px;
}
#buttons.rand2
{
    background: url(/hover_buttons.png) no-repeat;
    background-position: 0 -440px;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

秋日私语 2024-11-24 06:15:48

这里有几个问题。

  1. 范围 - 调用 mouseout 方法时变量 randomNum 不存在,因此仅添加您的类。

  2. 即使您将其设置为全局变量,如果您多次使用此代码,也无法解决您的问题(情况可能并非如此,因为您似乎正在使用 ID,但以防万一。)

  3. 即使您将其设置为全局变量,如果您多次使用此 这是 Math.round 而不是Math.Round

  4. 类不应仅由数字组成

我使用 jQuery 数据 api 来存储随机类名,以便您稍后可以检索它。这是您想要实现的目标的小提琴: http://jsfiddle.net/jomanlk/339c5/

$("#button_text").mouseenter(function() {
    var randClass = 'randomClass' + Math.round(Math.random() * 3);
    $("#buttons").addClass(randClass);
    $("#buttons").data('randClassValue', randClass);
}).mouseleave(function() {
    var randClass = $("#buttons").data('randClassValue');
    $("#buttons").removeClass(randClass)
});

<p id='button_text'>
    <a id='buttons'>Buttons</a>
</p>

There are a couple of issues here.

  1. Scoping - The variable randomNum does not exist when the mouseout method is called so your classes are only added.

  2. Even if you make it into a global variable, this won't solve your problem if you use this code multiple times (this might not be the case since you seem to be using IDs, but just in case.)

  3. It's Math.round and not Math.Round

  4. Classes shouldn't be composed of just a number

I've use the jQuery data api to store the random classname so you can retrieve it later. Here's a fiddle of what you're trying to achieve : http://jsfiddle.net/jomanlk/339c5/

$("#button_text").mouseenter(function() {
    var randClass = 'randomClass' + Math.round(Math.random() * 3);
    $("#buttons").addClass(randClass);
    $("#buttons").data('randClassValue', randClass);
}).mouseleave(function() {
    var randClass = $("#buttons").data('randClassValue');
    $("#buttons").removeClass(randClass)
});

<p id='button_text'>
    <a id='buttons'>Buttons</a>
</p>
萌化 2024-11-24 06:15:48

这里存在一个范围界定问题。确保您的随机参考可以通过这两种方法访问。下面我在方法之外声明变量,从 $.mouseenter() 方法更改其值,然后从 $.mouseleave() 方法再次访问它。

在我的示例中,我只是设置元素的文本,然后递增文本。您可以通过将对 $.text() 的两个调用更改为 $.addClass()$.removeClass() 来适应您的需求> 分别。

另请注意,不建议使用数字作为类名。也许您应该为值添加前缀。

var ranNum;

$("#hello")
  .mouseenter(function(){
   ranNum = Math.round(Math.random()*50);
    $(this).text(ranNum);
  })
  .mouseleave(function(){
   $(this).text(ranNum+1); 
  });

在线演示:http://jsbin.com/icuwav/edit

There's a scoping issue in play here. Make sure your random reference is accessible by both methods. Below I'm declaring my variable outside of my methods, changing its value from the $.mouseenter() method and then accessing it again from the $.mouseleave() method.

In my example I'm merely setting the text of an element, and then incrementing the text. You can adapt this to your needs by changing the two calls to $.text() to $.addClass() and $.removeClass() respectively.

Note also that it's not advised to use numbers as class names. Perhaps you should prefix the values.

var ranNum;

$("#hello")
  .mouseenter(function(){
   ranNum = Math.round(Math.random()*50);
    $(this).text(ranNum);
  })
  .mouseleave(function(){
   $(this).text(ranNum+1); 
  });

Online Demo: http://jsbin.com/icuwav/edit

甚是思念 2024-11-24 06:15:48

在将其添加为类之前尝试将其转换为字符串。

编辑:另外,正如其他人提到的那样, Math.Round 需要是 Math.round

try converting it to a string before adding it as a class.

edit: also, as others have mentions Math.Round needs to be Math.round

家住魔仙堡 2024-11-24 06:15:48

Math.Round 不是 javascript 中的方法。然而; Math.round

$(function() {
    $("#button_text").hover(function() {
        var randomNum = Math.round(Math.random()*3);
        $("#buttons").addClass(randomNum);
    }, function() {
        $("#buttons").removeClass(randomNum)
    });
});

Math.Round isn't a method in javascript. However; Math.round is.

$(function() {
    $("#button_text").hover(function() {
        var randomNum = Math.round(Math.random()*3);
        $("#buttons").addClass(randomNum);
    }, function() {
        $("#buttons").removeClass(randomNum)
    });
});
硬不硬你别怂 2024-11-24 06:15:48

你的 randomNum 在 mouseenter 函数中用 'var' 声明 - 它对 mouseleave() 不可见。

如果您的类名不以字母开头,您也可能会遇到问题 - 尽管数字可能“正常”,但我不认为它们是有效的类标识符。

Your randomNum is declared inside your mouseenter function with 'var' - it isn't visible to mouseleave().

You may also run into problems if your class names don't start with a letter - although numbers may "just work" I don't think they are valid class identifiers.

仅一夜美梦 2024-11-24 06:15:48

如果你想得到一个整数值使用Math.ceil

var randomNum = Math.ceil(Math.random()*3);

If you want to get an integer value use Math.ceil

var randomNum = Math.ceil(Math.random()*3);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文