如果条件不影响div的显示

时间:2015-12-29 03:37:11

标签: javascript jquery css if-statement nan

我在StackOverflow上搜索过,但没有任何问题符合我的特定困境。我有一个带有 foo 的div,它通过css设置为 display:inline-block

当变量 temp 是数字或字符串时,我希望foo可见,但当不是a时,不显示数字或字符串。

var temp;
        if (temp !== undefined || temp !== null) {
            temp = temp / 1000;
            temp = temp + 'K';
            $('.foo').css("display", "inline-block");
        }
        else {
            $('.foo').css("display", "none");
        }

当我运行此代码时,“foo”根本不会显示。

当我将代码更改为

var temp;
        if (temp != undefined || temp != null) {
            temp = temp / 1000;
            temp = temp + 'K';
            $('.foo').css("display", "inline-block");
        }
        if  (temp == undefined || temp == NaN) {
            $('.foo').css("display", "none");
        }

foo 仍设置为 显示:“无”

我在每个 上运行了console.log,如果 ,当 temp 是一个值时,它会通过第一个 if 以及 NaN null ,它确实会通过第二 。但为什么 显示:“无” 会生效?

提前谢谢你。 - VS

修改 在第一个代码块

中更改了第一个代码

要澄清,如果使用.get函数, temp 会不断重置为高于此值的新值:

var temp =this.model.get('custitem_temp');

并且多次成功设置此值。我的一些控制台供参考:

temp is set using .get: 80000
temp inside FIRST IF: 80000
temp is set using .get: undefined
temp inside FIRST IF: undefined
temp is set using .get: undefined
temp inside FIRST IF: undefined
temp is set using .get: 80000
temp inside FIRST IF: 80000
temp is set using .get: undefined
temp inside FIRST IF: undefined
temp is set using .get: 80000
temp inside FIRST IF: 80000
temp is set using .get: undefined

...

等等。

EDIT02:

感谢您的帮助。因此,根据建议,我的代码现在分为未定义

         if (temp === undefined || temp === null) {
            console.log("temp (should be undefined): " + temp)
            console.log("inside IF");;  
            $('.foo').css("display", "none");
         }
         else {
            console.log("temp (should be a value): " + temp)
            console.log("inside ELSE: " + temp);    
            temp = temp / 1000;
            temp = temp + 'K';
            $('.foo').css("display", "inline-block");
            console.log("after ELSE: " + temp);
        }

和控制台反映:

temp (should be a value): 80000
inside ELSE: 80000
after ELSE: 80K
temp (should be undefined): undefined
inside IF
temp (should be undefined): undefined
inside IF
temp (should be a value): 80000
inside ELSE: 80000
after ELSE: 80K
...

尽管如此,我仍然在foo上获得 display:none;

编辑03:

我添加了一个console.log来输出显示值:

            if (temp === undefined || temp === null) {
            console.log("temp (should be undefined): " + temp)
            console.log("inside IF");
            $('.foo').css("display", "none");
            console.log( "display value: " + $('.foo').css('display') );
         }
         else {
            console.log("temp (should be a value): " + temp)
            console.log("inside ELSE: " + temp);    
            temp = temp / 1000;
            temp = temp + 'K';
            $('.foo').css("display", "inline-block");
            console.log("after ELSE: " + temp);
            console.log( "display value: " + $('.foo').css('display') );
        }

我的控制台日志提供了一些有趣的见解: 像以前一样开始,除了说显示值是 未定义

temp (should be a value): 80000
inside ELSE: 80000
after ELSE: 80K
display value: undefined
temp (should be undefined): undefined
inside IF
display value: undefined
temp (should be undefined): undefined
inside IF

然后在大约9个循环后,它开始分配一个值来显示:

temp (should be a value): 80000
inside ELSE: 80000
after ELSE: 80K
display value: undefined
temp (should be undefined): undefined
inside IF
display value: none
temp (should be undefined): undefined
inside IF
display value: none
temp (should be a value): 80000
inside ELSE: 80000
after ELSE: 80K
display value: block
temp (should be undefined): undefined
inside IF
display value: none

有任何解释吗?

3 个答案:

答案 0 :(得分:2)

你检查过你的控制台了吗?您的第二个if语法错误。它应该是这样的

if (tempMileageWarranty == undefined || tempMileageWarranty == NaN)'

将粘贴错误复制到一边......

.foo设置为display: none;,因为temp未定义。

代码var temp;将使temp未定义,(传递第二个if),因为它永远不会被赋值。

这可能是一些有用的阅读:JavaScript checking for null vs. undefined and difference between == and ===

答案 1 :(得分:0)

t = typeof(temp);   
if(t == "undefined")
{
    console.log(temp +": do not display");  
}
else if(t == 'number' || t == 'string') {
    console.log(temp +": It is "+t+".So display the foo");
}

试试这段代码。我用3值temp.80000,“Foo”,未定义检查了它,我在控制台得到的结果是这样的。

80000: It is number.So display the foo
foo: It is string.So display the foo
undefined: do not display

我希望这会对你有所帮助。

答案 2 :(得分:0)

快速记录。想知道你是否注意到这里有双分号。

console.log("inside IF");;  

此外,我只是对temp定义的状态进行快速简单的检查,并确保它不为null。

if (temp && temp !== null) {
         console.log("temp (should be a value): " + temp)
        console.log("inside ELSE: " + temp);    
        temp = temp / 1000;
        temp = temp + 'K';
        $('.foo').css("display", "inline-block");
        console.log("after ELSE: " + temp);
 } else {
        console.log("temp (should be undefined): " + temp)
        console.log("inside IF");
        $('.foo').css("display", "none");
    }