将随机变量传递给 addClass
只是想生成一个随机数,然后将其作为类添加到现有的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这里有几个问题。
范围 - 调用 mouseout 方法时变量 randomNum 不存在,因此仅添加您的类。
即使您将其设置为全局变量,如果您多次使用此代码,也无法解决您的问题(情况可能并非如此,因为您似乎正在使用 ID,但以防万一。)
Math.round
而不是Math.Round
类不应仅由数字组成
我使用 jQuery 数据 api 来存储随机类名,以便您稍后可以检索它。这是您想要实现的目标的小提琴: http://jsfiddle.net/jomanlk/339c5/
There are a couple of issues here.
Scoping - The variable randomNum does not exist when the mouseout method is called so your classes are only added.
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.)
It's
Math.round
and notMath.Round
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/
这里存在一个范围界定问题。确保您的随机参考可以通过这两种方法访问。下面我在方法之外声明变量,从
$.mouseenter()
方法更改其值,然后从$.mouseleave()
方法再次访问它。在我的示例中,我只是设置元素的文本,然后递增文本。您可以通过将对
$.text()
的两个调用更改为$.addClass()
和$.removeClass()
来适应您的需求> 分别。另请注意,不建议使用数字作为类名。也许您应该为值添加前缀。
在线演示: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.
Online Demo: http://jsbin.com/icuwav/edit
在将其添加为类之前尝试将其转换为字符串。
编辑:另外,正如其他人提到的那样,
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 beMath.round
Math.Round 不是 javascript 中的方法。然而; Math.round是。
Math.Round isn't a method in javascript. However; Math.round is.
你的 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.
如果你想得到一个整数值使用Math.ceil
If you want to get an integer value use Math.ceil