您现在的位置是:首页 > 电脑技术查询 > web开发

一段js代碼,感覺判斷條件縱使為真。该怎么处理

编辑:chaxungu时间:2022-10-02 23:23:23分类:web开发

一段js代碼,感覺判斷條件縱使為真。
當系統時間是2007年5月10日。
函數依舊回執行到alert( "123 ")。
感覺if (showMonth=1)總是為true.
var lastDate = new Date();
//var teshu date
var showMonth=lastDate.getMonth() + 1
alert( "showMonth= " + showMonth)
alert(lastDate.getMonth())
if (showMonth=1)
{
alert( "123 ")
lastDate.setYear(lastDate.getYear() - 1)
lastDate.setMonth(11)

}
else
{
alert( "ddd ")
lastDate.setMonth(lastDate.getMonth())
}

------解决方案--------------------
if (showMonth=1) ==> if (showMonth == 1)
------解决方案--------------------
showMonth=1
改成
showMonth==1