编写一个打开和关闭灯泡的JavaScript代码。 语法:
null
img src = URl or img src = image_name.jpg
在这里,src属性设置或返回图像的src属性的值。必需的src属性指定图像的URL。 灯泡有两种使用状态,如下所述- 灯泡的初始状态(关闭状态):
点击灯泡(打开状态)后:
用于说明此主题的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> |
输出: 在点击灯泡之前- 点击灯泡后-
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END