JQuery / Javascript和&&的使用运营商 [英] JQuery/Javascript and the use of && operators

查看:115
本文介绍了JQuery / Javascript和&&的使用运营商的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用简单的条件语句来解决问题。失败的代码:

I'm trying to get a simple conditional statement to work, and running into problems. The failing code:

    $(document).ready(function(){
    var wwidth = $(window).width();

    if (wwidth < 321) {
        alert("I am 320 pixels wide, or less");
        window.scrollTo(0,0);
    } else if (wwidth > 321) && (wwidth < 481) {
        alert("I am between 320 and 480 pixels wide")
    }
});

如果删除了部分代码的else,我会收到警报。如果我尝试使用&&或||操作员会失败。我用谷歌搜索,我找不到它为什么不起作用的原因。我也尝试过:

If I remove the else if part of the code, I get the alert. If I try to use && or || operators it will fail. I've Googled, I can't find a reason why it's not working. I've also tried:

((wwidth > 321 && wwidth < 481))

以及其他方式,以防它是一些奇怪的语法。

along with other ways, just in case it's some odd syntax thing.

非常感谢任何帮助。谢谢:)

Any help would be greatly appreciated. Thanks :)

推荐答案

((wwidth > 321) && (wwidth < 481))

这是你需要的条件(http://jsfiddle.net/malet / wLrpt /).

This is the condition you need (http://jsfiddle.net/malet/wLrpt/).

我还会考虑让你的条件更加清晰:

I would also consider making your conditions clearer like so:

if (wwidth <= 320) {
    alert("I am 320 pixels wide, or less");
    window.scrollTo(0,0);
} else if ((wwidth > 320) && (wwidth <= 480)) {
    alert("I am between 320 and 480 pixels wide")
}

这篇关于JQuery / Javascript和&amp;&amp;的使用运营商的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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