Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery-如果值等于其中任何一个,则执行此操作_Jquery - Fatal编程技术网

jQuery-如果值等于其中任何一个,则执行此操作

jQuery-如果值等于其中任何一个,则执行此操作,jquery,Jquery,我有以下代码: if(weather.temp >= 30) { $("body").randomClass( sunny ); $("body").addClass( 'sunny' ); randno = sunnyquotes[Math.floor( Math.random() * sunnyquotes.length )]; $('.home-main h1').text( randno ); } else if (weather.todayC

我有以下代码:

if(weather.temp >= 30) {

    $("body").randomClass( sunny );
    $("body").addClass( 'sunny' );
    randno = sunnyquotes[Math.floor( Math.random() * sunnyquotes.length )];
    $('.home-main h1').text( randno );
  } else if (weather.todayCode == 40||39||38||37||35||9||4||3||11) {
    $("body").randomClass( rain );
    $("body").addClass( 'rain' );
    randno = rainquotes[Math.floor( Math.random() * rainquotes.length )];
    $('.home-main h1').text( randno );
  } else if ((weather.temp > 18) && (weather.temp < 30) && (weather.todayCode == 32||30||34)) {
    $("body").randomClass( moderate );
    $("body").addClass( 'moderate' );
    randno = moderatequotes[Math.floor( Math.random() * moderatequotes.length )];
    $('.home-main h1').text( randno );
  } else if (weather.temp <= 18) {
    $("body").randomClass( cold );
    $("body").addClass( 'cold' );
    randno = coldquotes[Math.floor( Math.random() * coldquotes.length )];
    $('.home-main h1').text( randno );

  }
我检查多个数字的方式显然是错误的


任何帮助都将不胜感激。

实现多重比较的一种简单方法是使用数组

if ([40,39,38,37,35,9,4,3,11].indexOf(weather.todayCode) > -1)

实现多重比较的一种简单方法是使用数组

if ([40,39,38,37,35,9,4,3,11].indexOf(weather.todayCode) > -1)

比较结果写得不正确。试试这个:

...
else if (weather.todayCode == 40 || weather.todayCode == 39 || weather.todayCode == 38) //Add more conditions as necessary
...

更好的方法是使用一个可以循环通过有效值数组的函数—可读性更强,但上面的示例将解决您眼前的问题。

比较写得不正确。试试这个:

...
else if (weather.todayCode == 40 || weather.todayCode == 39 || weather.todayCode == 38) //Add more conditions as necessary
...

一个更好的主意是使用一个可以循环通过有效值数组的函数-可读性更强,但上面的示例将解决您眼前的问题。

您也可以使用$。inarray

rainCode=[ 40,39,38,37,35,9,4,3,11];
if ($.inArray(weather.todayCode, rainCode) > -1)
     $("body").addClass( 'rain' );

您还可以使用$。inarray

rainCode=[ 40,39,38,37,35,9,4,3,11];
if ($.inArray(weather.todayCode, rainCode) > -1)
     $("body").addClass( 'rain' );

很好+1使用indexOf,我没想到!很好+1使用indexOf,我没想到!