javascript - 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>
解决方案
如果你正确地检查了检查,你会发现你的 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>
推荐阅读
- python - 停止在 Twilio 中自动调整 MMS 大小
- javascript - 未处理的 Promise Rejection:在条件下集成异步功能
- material-ui - 材质 UI:手动突出显示 xgrid 行
- forms - 表单识别器标签编辑器中未显示的图像
- cypress - Cypress.log 突出显示多个元素
- kubernetes - 如何为基于 Helm 的 Hazelcast 部署定义 MapStore
- amazon-web-services - 如何在无服务器应用程序中使用 middy 将 co-relationId 添加到 pino 记录器
- c++ - vector::erase 失败,二进制表达式的操作数无效(T 和 const T)
- excel - 如何为文档级 VSTO 模板创建安装文件?
- c# - 在 kafka 中手动提交偏移量