javascript:否则,如果条件不起作用 [英] javascript: else if condition not working

查看:66
本文介绍了javascript:否则,如果条件不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用的按钮,单击后将div移至左侧.在脚本中,"if"条件可以正常工作,但"if else"条件不能正常工作.

The button that I'm working on, when clicked, pans a div to the left. In the script, the "if" condition works fine but the "if else" condition doesn't.

我是否忘记了要包含在脚本中的内容?

Did I forgot something to include in the script?

这是我的脚本:

<script>
    function panleft(el){
        var elem = document.getElementById(el);

        if (elem.style.left = "90%"){   
            elem.style.transition = "left 0.3s ease-in 0s";
            elem.style.left = "70%";
            }

        else if (elem.style.left = "70%"){  
            elem.style.transition = "left 0.3s ease-in 0s";
            elem.style.left = "90%"
            }

    }

</script>

推荐答案

在Javascript中,等于(=)运算符的工作方式如下:

In Javascript, the equal (=) operators work as follows:

  • x = y 是赋值(即赋予变量X值Y )
  • x == y 是一个宽松的相等性测试(即x等于y ),它是宽松的,因为它确实强制类型化(更改类型以尝试并找到匹配项),例如,如果"1" == 1(字符串与数字),则Javascript会将其转换为另一种格式,直到找到匹配项或失败为止.
  • x === y 是严格相等的.(是x等于y ),而无需更改原始类型.
  • x = y is assignation (i.e. give variable X value Y)
  • x == y is a loose equality test (i.e. is x equal to y), it's loose because it does type coercion (changes types to try and find a match), such as if "1" == 1 (string vs number), Javascript will convert one to the other format until it finds a match or has failed to do so.
  • x === y is strict equality. (is x equal to y) WITHOUT changing primitive type.

在您的示例中,您要分配值,而不是测试相等性.那就是你问题的根源.

In your example you're assigning values, not testing equality. That's the source of your problems.

此外,您的"if"语句也无法真正起作用,因为您也在进行赋值,而不是相等性测试.

Also, your "if" statement is not truly working, since you're also making an assignation, not an equality test.

这篇关于javascript:否则,如果条件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆