在jquery中使6个变量中的1个随机为真

发布于 2024-12-11 15:40:13 字数 1410 浏览 0 评论 0原文

我试图使用 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 技术交流群。

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

发布评论

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

评论(1

魂ガ小子 2024-12-18 15:40:13

我怀疑这确实是一个 XY 问题,这解决了标题,但可能会错过“最终真正想要的是什么”。无论如何,这些概念都有一定的适应性。


我不会使用变量,而是使用数组/对象。

让我们假设一个对象(这样我们可以使用不同的名称:-),然后是一个“控制”序列,其属性可以切换:

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

但是,如果由于某种原因确实需要变量,则闭包可以使用(这也可以用于为特定绑定运行任意代码):

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)

快乐编码。

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)

Happy coding.

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