有5个没有.按钮(图像).最初都是关闭图像.一次只能打开1个.所以,当我按下img的src变为on.png的任何按钮时.然后,当我按下任何打开或关闭按钮时,按下的按钮源img变为on.png,而img上的所有其他按钮也变为off.png.
HTML代码是,
<table cellspacing="0" style="padding:0%; margin:0% auto;">
<tr><td><img id="img1" src="off.png" height="30" width="30" onclick="off(this.id);" /></td><td>45:78</td></tr>
<tr><td><img id="img2" src="off.png" height="30" width="30" onclick="off(this.id);" /></td><td>45:78</td></tr>
<tr><td><img id="img3" src="off.png" height="30" width="30" onclick="off(this.id);" /></td><td>45:78</td></tr>
<tr><td><img id="img4" src="off.png" height="30" width="30" onclick="off(this.id);" /></td><td>45:78</td></tr>
<tr><td><img id="img5" src="off.png" height="30" width="30" onclick="off(this.id);" /></td><td>45:78</td></tr>
</table>
Run Code Online (Sandbox Code Playgroud)
javascript代码是,
function off(a)
{
var images = document.getElementsByTagName('img');
for(var i = 0; i < images.length; i++)
{
var img = images[i];
alert(img.src);
if(img.src == 'on.png')
{
img.src = 'off.png';
}
}
document.getElementById(a).src='on.png';
}
Run Code Online (Sandbox Code Playgroud)
if()条件不起作用,请提供解决方案并解释其原因
工作.谢谢!
img.src将返回图像的完整路径(/full/path/to/on.png),而不是标记(on.png)中src属性的设置.而是使用:
if (img.getAttribute('src') === 'on.png')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8607 次 |
| 最近记录: |