在jquery中使6个变量中的1个随机为真
我试图使用 jquery 使其随机出现一系列 div 中的一个以及它的导航链接(即,如果服务被选中,服务链接将不会消失)。我在这个论坛上多次以各种形式找到了这段代码,并且想知道是否以及如何将其改编为我想要的。
var services = $(random1, random2, random3).get()
.sort(function(){return Math.round(Math.random());}).slice(0,1)
$(services)/*Conditions here*/;
var random1 = false;
var random2 = false;
var random3 = false;
我知道,这是一个非常糟糕的例子。我迷失了它。任何帮助将不胜感激,并提前致谢。
编辑:我之前确实尝试过进行更简单的比较,但这就是我实际上正在做的事情。我尝试改编来自@pst 的代码。
var v1 = "hello"
var v2 = "world"
var control = [
function (v) { v1 = v },
function (v) { v2 = v }
]
$.each(control, function (i, fn) {
fn(false)
})
$("a#random-btn").click(function(event){
event.preventDefault();
var trueIdx = Math.floor(control.length * Math.random())
props[trueIdx](true)
if (v1 === true){
$("div#small-obstacles-contain a#1 span").stop().animate({opacity: 1,}, '100').animate({opacity: 0,}, '100');
$("div#small-obstacles-contain a#2 span").stop().animate({opacity: 1,}, '100').animate({opacity: 0,}, '100');
}
if (v2 === true){
$("div#small-obstacles-contain a#3 span").stop().animate({opacity: 1,}, '100').animate({opacity: 0,}, '100');
$("div#small-obstacles-contain a#4 span").stop().animate({opacity: 1,}, '100').animate({opacity: 0,}, '100');
}
});
I am trying to make a one of a series of divs appear randomly with jquery along with it's navigation link(i.e. If services gets pick, the services link will unfade). I have found this code various times in various forms on this forum, and was wondering if and how I could adapt it to what I would want.
var services = $(random1, random2, random3).get()
.sort(function(){return Math.round(Math.random());}).slice(0,1)
$(services)/*Conditions here*/;
var random1 = false;
var random2 = false;
var random3 = false;
This is a really bad example, I know. I am get lost on it. Any help would be greatly apprecaited, and thanks in advance.
EDIT: I did try to make an easier comparison earlier, but here is what I am actually working on. I tried to adapt the code from @pst.
var v1 = "hello"
var v2 = "world"
var control = [
function (v) { v1 = v },
function (v) { v2 = v }
]
$.each(control, function (i, fn) {
fn(false)
})
$("a#random-btn").click(function(event){
event.preventDefault();
var trueIdx = Math.floor(control.length * Math.random())
props[trueIdx](true)
if (v1 === true){
$("div#small-obstacles-contain a#1 span").stop().animate({opacity: 1,}, '100').animate({opacity: 0,}, '100');
$("div#small-obstacles-contain a#2 span").stop().animate({opacity: 1,}, '100').animate({opacity: 0,}, '100');
}
if (v2 === true){
$("div#small-obstacles-contain a#3 span").stop().animate({opacity: 1,}, '100').animate({opacity: 0,}, '100');
$("div#small-obstacles-contain a#4 span").stop().animate({opacity: 1,}, '100').animate({opacity: 0,}, '100');
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我怀疑这确实是一个 XY 问题,这解决了标题,但可能会错过“最终真正想要的是什么”。无论如何,这些概念都有一定的适应性。
我不会使用变量,而是使用数组/对象。
让我们假设一个对象(这样我们可以使用不同的名称:-),然后是一个“控制”序列,其属性可以切换:
但是,如果由于某种原因确实需要变量,则闭包可以使用(这也可以用于为特定绑定运行任意代码):
快乐编码。
I suspect this is really an X-Y problem, this address the title, but may miss "what is really desired at the end of the day". In any case, the concepts are somewhat adaptable.
I wouldn't use variables, but rather an an array/object.
Let's assume an object (so we can have different names used :-) and then a "control" sequence for which properties are eligible to be toggled:
However, if variables were really desired for some reason then closures could be used (this could also be used to run arbitrary code for a particular binding):
Happy coding.