首页 > 解决方案 > Some Javascript codes not working, only jquery codes

问题描述

I have an external javascript file where I wrote jquery codes and javascript codes but the javascript codes aren't working only the alert is working.

I want some texts to flash different colors but it doesn't.

When I inspect elements using ctrl+shift+i I see the colors changing under inspect element, but it doesn't change in the webpage.

Something like it's running on the background or what I really don't know.

function flash() { var text = document.getElementById('sales'); text.style.color=(text.style.color=='red')? 'green' : 'red'; } var clr = setInterval(flash, 500)
<p id="sales"> <h3 style ="text-align: center; font-weight: bold;"> Flash Sales </h3></p>

标签: javascriptjquery

解决方案


如果你正确地检查了检查,你会发现你的 HTML 与你所拥有的不同。<p>元素不能包含元素<h3>,并且 DOM 已更改为<p id="sales"></p><h3>...</h3>.

因此,您的 CSS 被应用到了错误的位置。

function flash() { var text = document.getElementById('sales'); text.style.color=(text.style.color=='red')? 'green' : 'red'; } var clr = setInterval(flash, 500)
<h3 id="sales" style ="text-align: center; font-weight: bold;"> Flash Sales </h3>


推荐阅读