JavaScript 更改 HTML 属性值

AI 摘要

文章说明 JavaScript 可通过 DOM 方法获取 HTML 元素并修改其属性值。示例中点击按钮时,使用 document.getElementById 获取图片元素,再更改其 src 属性,从而切换灯泡图片,实现页面内容的动态更新。

<button onclick="document.getElementById('myImage').src='https://img.8i5.net/image/bulboff.gif'">Turn on the light</button>

<img id="myImage" src="https://img.8i5.net/image/bulboff.gif" style="width:100px">

<button onclick="document.getElementById('myImage').src='https://img.8i5.net/image/bulboff.gif'">Turn off the light</button>


JavaScript 更改了标记的src(source) 属性的值:<img>

示例:

JavaScript 更改 HTML 属性值-李联华的博客网

评论