更改 if else 语句中的 Jquery 变量

发布于 2024-12-13 01:55:40 字数 728 浏览 1 评论 0原文

---原始代码----

$(function () {

    var options = {
    xaxis: { mode: "time", tickLength: 5 },
    selection: { mode: "x" },
    crosshair: { mode: "x" }
    };

 });

rlt=true时,我希望crosshair: { mode: "x" }位于选项变量内部>。

以下是我的代码,但它不正确。有人可以指出错误吗?

谢谢!

$(function () {
    var rlt = true;
    var options;
    if(rlt){
        options = {
        xaxis: { mode: "time", tickLength: 5 },
        selection: { mode: "x" },
        crosshair: { mode: "x" }
        };
    }else{
        options = {
        xaxis: { mode: "time", tickLength: 5 },
        selection: { mode: "x" }
        };  

    }
 });

---Original code----

$(function () {

    var options = {
    xaxis: { mode: "time", tickLength: 5 },
    selection: { mode: "x" },
    crosshair: { mode: "x" }
    };

 });

I want crosshair: { mode: "x" } to be inside of options variable when rlt=true.

Following is my code, but it's not correct.Is there anyone can point error out?

Thanks!

$(function () {
    var rlt = true;
    var options;
    if(rlt){
        options = {
        xaxis: { mode: "time", tickLength: 5 },
        selection: { mode: "x" },
        crosshair: { mode: "x" }
        };
    }else{
        options = {
        xaxis: { mode: "time", tickLength: 5 },
        selection: { mode: "x" }
        };  

    }
 });

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

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

发布评论

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

评论(2

剪不断理还乱 2024-12-20 01:55:40

对我有用,但你可以这样写:

var rlt = true;
var options = {
    xaxis: { mode: "time", tickLength: 5 },
    selection: { mode: "x" }
    };  

if (rlt) options.crosshair = { mode: "x" }

works for me, but you can write it this way:

var rlt = true;
var options = {
    xaxis: { mode: "time", tickLength: 5 },
    selection: { mode: "x" }
    };  

if (rlt) options.crosshair = { mode: "x" }
纸短情长 2024-12-20 01:55:40

如果您在 $(function () {} 内检查 options.crosshair.mode ,您所拥有的将有效
变量的作用域在哪里。

What you have works if you are checking options.crosshair.mode inside the $(function () {}
where the variable is scoped.

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