问题描述
我正在尝试使一系列div中的一个与jquery及其导航链接一起随机出现(即,如果服务被选中,则服务链接将消失).我在这个论坛上以各种形式多次找到了此代码,并且想知道是否以及如何使它适应我的需求.
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.
我确实尽早做出了一个简单的比较,但这是我实际上正在做的事情.我试图改编@pst中的代码.
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');
}
});
推荐答案
我怀疑这确实是一个X-Y问题,它解决了标题,但可能会错过一天结束时真正想要的".在任何情况下,这些概念都具有一定的适应性.
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:
var obj = {a: true, b: false, "3": false, hello: "world"}
var control = ["a", "b", "3"]
// set all to false -- noet that $.each != $().each !!!
$.each(control, function (i, prop) {
obj[prop] = false
})
// set one true
var trueIdx = Math.floor(control.length * Math.random())
obj[control[trueIdx]] = true
但是,如果出于某些原因确实需要变量,则可以使用闭包(也可以将其用于特定绑定的任意代码):
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):
var v1 = "hello"
var v2 = "world"
var control = [
function (v) { v1 = v },
function (v) { v2 = v }
]
// set all to false -- noet that $.each != $().each !!!
$.each(control, function (i, fn) {
fn(false)
})
// set one true
var trueIdx = Math.floor(control.length * Math.random())
props[trueIdx](true)
快乐的编码.
这篇关于在jQuery中随机使6个变量中的1个为真的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!