if else语句中更改Jquery变量

时间:2011-11-02 12:16:01

标签: jquery jquery-ui jquery-plugins

---原始代码----

$(function () {

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

 });

rlt = true 时,我希望十字准线:{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" }
        };  

    }
 });

2 个答案:

答案 0 :(得分:6)

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

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

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

答案 1 :(得分:1)

如果您在options.crosshair.mode内查看$(function () {},那么您的工作有效 变量的作用域。