iT邦幫忙

0

https://ithelp.ithome.com.tw/questions/10205651
關於這篇文章,請問只執行一次藍紅就停止,該修改哪裡達到無限循環?

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Language" content="zh-tw">
<meta http-equiv="Content-Type" content="text/html; charset=big5">
<title>測試</title>

<script>
setInterval(function(){blink.color='#0000ff'?'red':'blue'},300)
</script>
</head>

<body>
<blink>
<font face="標楷體" color="blue" id="blink" style="font-size:36pt">
	
	測試
	
</font>
</blink>
		
</body>
</html>
個人不喜歡 JS,這邊單純用 CSS 給樓主參考了
https://codepen.io/jonesleeinfo/pen/dyzoVmm
Mao iT邦新手 1 級 ‧ 2021-11-12 23:03:23 檢舉
  
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
Mao
iT邦新手 1 級 ‧ 2021-10-13 15:19:20

blink 文字閃爍語法(已過時被棄用)

樓下 Homura 的方法比較好~我多宣告了一個變數,感覺繞了一下路,已修正

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Language" content="zh-tw">
    <meta http-equiv="Content-Type" content="text/html; charset=big5">
    <title>測試</title>
    <script>
    setInterval(function(){blink.color=blink.color==="blue"?"red":"blue"},300)
    </script>
  </head>

  <body>
    <blink>
      <font face="標楷體" color="blue" id="blink" style="font-size:36pt">

        測試

      </font>
    </blink>
  </body>
</html>

發錯位置,不好意思

2
Homura
iT邦高手 1 級 ‧ 2021-10-13 15:45:49

setInterval(function(){blink.color='#0000ff'?'red':'blue'},300

你的if else判別式明顯有問題....
if條件只有'#0000ff'
'#0000ff'因為是靜態的結果永遠都是true
只會一直進入if條件
怎麼樣都跑不到else條件
改成跟自己color比較這樣就OK了

setInterval(function(){blink.color=blink.color==='blue'?'red':'blue'},300)
Mao iT邦新手 1 級 ‧ 2021-10-13 15:56:15 檢舉

看來我另外宣告變數多此一舉了~

Homura iT邦高手 1 級 ‧ 2021-10-13 16:09:06 檢舉

毛毛
我覺得你那樣寫還蠻清楚的XD

我要發表回答

立即登入回答