javascript - 如果我的页面上的文本消失,如何向我的图像添加命令?
问题描述
我正在制作一个 HTML 页面来显示数字信息,我将数字与另一个图像一起输出到页面中,我想在数字输出下显示该图像。
文本在等于 null/0 时自动消失,我希望图像在数字消失时消失,我已经在 JS 脚本编写器的帮助下更新了代码,但是它没有解决我的问题,因为代码中的数字发生了变化并且JS 脚本只能运行一次。如果有任何帮助,数据将通过异步功能输出到页面中。
一段代码(JS&HTML):
<body>
<img class="batset battery-img" src="img/bat.png"> <!-- this is the image I want to disappear when the numbers = 0.00 or have no output -->
<p id="pp-lable" class="text glow battery-output">0</p> <!--these numbers already disappear when they = 0.00 or have no output (these numbers change as it links to another JS which links to a pogram, so a JS that is shown in this snippet wont work as it does not repeat it self to detect the numbers that change-->
<pre id="hit-count-lable" class="text-hit-count">0x100 0x50 0xMiss</pre> <!--this does not need to be modified or made to disappear it is fine as long as the numbers above do it-->
<script>
let batteryIMG = document.querySelector('.battery-img')
let output = document.querySelector('.battery-output');
let batteryNum = document.querySelector('.battery-num')
if(output.innerHTML === '0' || output.innerHTML === '' &&
batteryNum.innerHTML === '' || batteryNum.innerHTML === '') {
batteryIMG.style.display = 'none';
output.style.display = 'none';
batteryNum.style.display = 'none';
}
</script>
更新 1:我使用此代码使图像和文本消失,但我不知道如何使它们重新出现并使脚本保持循环。
更新2:所以我得到了一位朋友的帮助,他发现了一个已经内置在javascript中的循环,他只是为图像和数字制作了2个变量,然后构建了允许图像在文本=时消失的脚本到 0。感谢大家在线程中的贡献,我现在将关闭这个线程作为回答并给出最好的答案给神秘的影子,因为我们在 js 中实现的代码是由她建议的。
<body>
<img class="batset battery-img" src="img/bat.png"/>
<p id="pp-lable" class="text glow battery-num"></p>
<pre id="hit-count-lable" class="text-hit-count">0x100 0x50 0xMiss</pre>
<script>
/*currently not working*/
let batteryIMG = document.querySelector('.battery-img') // battery
let batteryNum = document.querySelector('.battery-num') // number in battery
if(batteryNum.innerHTML === '' || batteryNum.innerHTML === '') {
batteryIMG.style.display= 'none'
batteryNum.style.display = 'none';
}
else {
(batteryNum.innerHTML === '' || batteryNum.innerHTML === ''); {
batteryIMG.style.display = 'none'
batteryNum.style.display = 'none';
}
}
解决方案
首先为您的图像添加一个ID
然后在括号后添加 document.getElementById('MyImg').classList.add('hidden') 如下
let batteryIMG = document.querySelector('.battery-img')
let output = document.querySelector('.battery-output');
let batteryNum = document.querySelector('.battery-num')
if(output.innerHTML === '0' || output.innerHTML === '' &&
batteryNum.innerHTML === '' || batteryNum.innerHTML === '') {
document.getElementById('MyImg').classList.add('hidden') // Add this line
batteryIMG.style.display = 'none';
output.style.display = 'none';
batteryNum.style.display = 'none';
}
推荐阅读
- c# - 多视图和 MaintainScrollPositionOnPostback
- javascript - 通过外部交互更新 React 组件状态
- html - 删除表格之间的间距
- azure - 如何设置我的 Azure 帐户以通过 REST API 进行 ARM 部署?
- laravel - 在 laravel 5.5 中不显示 Flash 消息
- python - 如何使用 for 循环改进子图部分?
- android - 将 Firebase 应用推送到应用商店后如何对其进行迭代?
- javascript - 如何在 React-Native 中从 firebase 返回一组孩子
- mysql - MySQL + EF 更新很慢?
- objective-c - 我们什么时候需要将框架添加到 Xcode 项目中?