【问题标题】:if condition not affecting display of div如果条件不影响 div 的显示
【发布时间】:2016-04-02 22:10:22
【问题描述】:

我在 StackOverflow 上进行了搜索,但没有任何问题符合我的具体困境。我有一个类 foo 的 div,它通过 css 设置为 display: inline-block。 p>

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

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 仍然全部设置为 display:"none"

我在每个 if 上运行 console.log,当 temp 是一个值时,它确实会通过first if 以及当它是 NaNnull em> 它确实经历了 second if。但是为什么ONLY display: "none" 会生效呢?

提前谢谢你。 - 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:

感谢您的所有帮助。因此,根据建议,我的代码现在正在划分 undefinedvalues

         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
...

尽管如此,我仍然得到 display: none; on foo

编辑 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') );
        }

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

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

有什么解释吗?

【问题讨论】:

  • 你在哪里为temp赋值?
  • 这是否包含在 .ready() 函数中?
  • var temp;temp 的值设置为null。 (除非您在两者之间分配值)。
  • 我在 if 条件之前被赋值给 temp...这是使用 NetSuite 的专有系统的更改版本。不需要 .ready() ,因为这是通过 gulp 缩小到主 js 文件然后被加载的。 “temp”在页面上多次加载值,此脚本用于评估“foo”在页面上的显示。
  • @AndrewBrooke,是的,我只是想说,它相当于null,所以temp != undefined || temp != null 应该始终为假。

标签: javascript jquery css if-statement nan


【解决方案1】:

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

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

把复制粘贴错误放在一边……

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

代码var temp; 将使temp 未定义(传递您的第二个if),因为它从未被赋值。

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

【讨论】:

  • 对不起语法,在复制和编辑时,我捏造了第二个 if。我编辑它是正确的。我检查控制台。当“temp”有一个值时,它会通过我的第一个 if,然后当它未定义时,它会通过第二个 if。但是无论 temp 有值,它仍然不显示。
  • 我不知道你为什么会感到困惑,它被设置为display: none;,因为它是未定义的..
  • 但是当它被定义时,它仍然将其设置为显示:无...这是每次通过 .get 方法将 temp 设置为新值时进行评估
  • 谢谢你的小提琴。我想我的系统中还有其他东西无法正常工作,因为我的代码仍然无法正常工作。我经常只被发送到第一个 if...temp 是否未定义。参考我的console.log in OP。
【解决方案2】:
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");
}

试试这个代码。我用 temp.80000,"Foo",undefined 的 3 个值检查它,我在控制台中得到的结果是这样的。

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

希望对你有所帮助。

【讨论】:

  • 谢谢...所以我实现了你所说的,我仍然得到相同的结果,所有显示:无:我的一些控制台日志:++ 80000:它是数字。所以显示 foo / temp(应该是一个值):80000 / inside ELSE: 80000 / after ELSE: 80K / display value: undefined / undefined: 不显示 / temp (应该是未定义的): undefined / inside IF / display value : undefined / undefined: do not display /++ 可以看到显示的css值是"undefined" 那就是我没有得到的部分。
  • 你是动态创建div吗?
  • 好的。我问这个是因为不存在的 div 的显示属性是“未定义的”。如果脚本确实找到了任何 div,它会将其 display 属性返回为未定义。但如果你说 div 已经存在,那么请不要介意。
【解决方案3】:

快速笔记。想知道您是否注意到这里的双分号。

console.log("inside IF");;  

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

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");
    }

【讨论】:

  • 我在发布后注意到了双分号。我纠正了这一点,但这并没有解决任何问题。如果您查看我对 OP 的最后一次编辑,我正在检查(和控制台日志记录)foo 的显示状态...它一开始是未定义的,然后在大约 9 次迭代后它收到一个状态(块/无)
  • 尝试使用 show() hide() 来代替 jQuery css 函数。它们将达到相同的效果,但可能更适合您尝试做的事情。 “显示内联块并隐藏无”。
猜你喜欢
  • 2019-08-09
  • 1970-01-01
  • 1970-01-01
  • 2011-07-09
  • 1970-01-01
  • 2012-12-17
  • 2019-05-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多