0
我最终试图做的是有一个框和一个图像隐藏最初取决于其他功能的结果,然后取消隐藏框(通过给它一个背景色)和图像以及根据被调用的函数更改图像。隐藏/更改/添加图像
我有两个问题,下面的代码:
- 首先是在执行时出现的框,但图像做 没有。
- 其次,当其中一个函数被第二次调用(它在第一次调用时抛出没有 错误)由脚本的其余部分(这是一个计算器如此 它会发生多次),然后我得到一个错误,如果图像没有 不再存在:
"JavaScript runtime error: Unable to set property 'src' of undefined or null reference"
的JScript:
function one(input) {
document.getElementById("result").style.background = "lightgray";
document.getElementById("result").style.color = "green";
document.getElementById("icon").src = "tick.png";
document.getElementById("icon").style.visibility = "visible";
}
function two(input) {
document.getElementById("result").style.background = "lightgray";
document.getElementById("result").style.color = "red";
document.getElementById("icon").src = 'cross.png';
document.getElementById("icon").style.visibility = "visible";
}
HTML:
<div id="result">
<img src="tick.png" id="icon" style="visibility:hidden">
</div>
和CSS(疑问是,虽然需要):
#result {
width: 820px;
height: 450px;
border-radius: 20px;
padding-top:25px;
padding-bottom: 25px;
color: green;
font-size:40pt;
text-align: center;
margin-top: 100px;
margin-right: 150px;
margin-left:420px;
}
#result img{
margin-top: 85px;
width:220px;
height: 190px;
}
当你得到那个错误,你还能看到DOM检查器中的'icon'元素吗? – Barmar
jsfiddle链接:http://jsfiddle.net/h5XF2/ – oomlaut
我无法复制你的错误。请看这个小提琴:http://jsfiddle.net/jDVjP/1/ – oomlaut