使用JavaScript打开或关闭灯泡

编写一个打开和关闭灯泡的JavaScript代码。 语法:

null
img src = URl
     or
img src = image_name.jpg

在这里,src属性设置或返回图像的src属性的值。必需的src属性指定图像的URL。 灯泡有两种使用状态,如下所述- 灯泡的初始状态(关闭状态): 图片[1]-使用JavaScript打开或关闭灯泡-yiteyi-C++库

点击灯泡(打开状态)后: 图片[2]-使用JavaScript打开或关闭灯泡-yiteyi-C++库

用于说明此主题的JavaScript代码:

<html>
<body>
<!-- onclick event is generated
and it calls turnOnOff function -->
<!-- OFFbulb.jpg is the turn off bulb image -->
<img id= "Image" onclick= "turnOnOff()"
<p>Click on the bulb to turn it ON and OFF</p>
<script>
// implementation of turnOnOff function -->
function turnOnOff()
// taking image in image variable
var image = document.getElementById( 'Image' );
//Match the image name
//whether it is ONbulb or OFFbulb
//change image to OFFbulb.jpg if
//it match with ONbulb otherwise
//change it to ONbulb.jpg -->
if (image.src.match( "ONbulb" ))
wp-content/uploads/OFFbulb.jpg" ;
else
wp-content/uploads/ONbulb.jpg" ;
}
</script>
</body>
</html>


输出: 在点击灯泡之前- 图片[3]-使用JavaScript打开或关闭灯泡-yiteyi-C++库 点击灯泡后- 图片[4]-使用JavaScript打开或关闭灯泡-yiteyi-C++库

© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享