猿问

一段JS代码,在在<script>标签内好用,放在单独文件(.js)里就不好用了

<strong id="syhw"></strong>

<script>

var hours =  (new Date()).getHours();

var syhw1 = document.getElementById("syhw");

if(hours<10&&hours>=8){

    syhw1.innerHTML="13";


}

else if(hours<12&&hours>=10)

{//预约剩余10

    syhw1.innerHTML="10";

}

else if(hours<16&&hours>=12)

{//预约剩余7

    syhw1.innerHTML="7";


}

else

{

syhw1.innerHTML="5";

};

</script>

把这端放到js里面就不起作用了怎么办,难道必须用jq写吗

慕桂英4014372
浏览 905回答 1
1回答

慕勒3428872

有什么问题?我直接把你的代码分开在单独的js中也是没问题的。test.html<!DOCTYPE&nbsp;html><html><head> &nbsp;&nbsp;&nbsp;&nbsp;<title>test</title></head><body> &nbsp;&nbsp;&nbsp;&nbsp;<strong&nbsp;id="syhw"></strong> &nbsp;&nbsp;&nbsp;&nbsp;<script&nbsp;src='test.js'></script></body></html>test.jsvar&nbsp;hours&nbsp;=&nbsp;&nbsp;(new&nbsp;Date()).getHours();var&nbsp;syhw1&nbsp;=&nbsp;document.getElementById("syhw");if(hours<10&&hours>=8)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;syhw1.innerHTML="13"; }&nbsp;else&nbsp;if(hours<12&&hours>=10)&nbsp;{//预约剩余10 &nbsp;&nbsp;&nbsp;&nbsp;syhw1.innerHTML="10"; }&nbsp;else&nbsp;if(hours<16&&hours>=12)&nbsp;{//预约剩余7 &nbsp;&nbsp;&nbsp;&nbsp;syhw1.innerHTML="7"; }&nbsp;else&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;syhw1.innerHTML="5"; }
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答