改变图像,运用match方法判断

时间:2024-10-21 17:35:50

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
</head>
<body>

<script>
function changeImage()
{
element=document.getElementById('myimage')
if (element.src.match("bulboff"))//JavaScript String对象 Match()方法  ,如果匹配到bulboff字符,返回TRUE,否则返回FALSE
{
element.src="/images/pic_bulbon.gif";
}
else
{
element.src="/images/pic_bulboff.gif";
}
}
</script>
<img id="myimage" onclick="changeImage()"
src="/images/pic_bulboff.gif" width="100" height="180">
<p>点击灯泡就可以打开或关闭这盏灯</p>

</body>
</html>