如何通过JavaScript修改元素的src属性值为长尾关键词?
- 内容介绍
- 文章标签
- 相关推荐
本文共计414个文字,预计阅读时间需要2分钟。
JavaScript修改src属性值的方法:
1.使用setAttribute()方法,语法为:`元素对象.setAttribute('src', 属性值)`
2.利用HTML+DOM的src属性,语法为:`元素对象.src=属性值`
操作环境:Windows 7系统
javascript改变src属性值的方法:1、使用setAttribute()方法,语法“元素对象.setAttribute("src","属性值")”;2、利用HTML DOM的src属性,语法“元素对象.src="属性值"”。
本教程操作环境:windows7系统、javascript1.8.5版、Dell G3电脑。
javascript改变src属性值
方法1:使用setAttribute()方法
setAttribute() 方法添加指定的属性,并为其赋指定的值。
如果这个指定的属性已存在,则仅设置/更改值。
示例:
<!DOCTYPE html> <html> <body> <img src="img/1.jpg" width="200"/> <p id="demo">点击按钮来改变img标签src属性的值。</p> <button onclick="myFunction()">试一下</button> <script> function myFunction() { document.getElementsByTagName("img")[0].setAttribute("src","img/2.jpg"); } </script> </body> </html>
效果图:
2、使用HTML DOM对象的src属性
<!DOCTYPE html> <html> <body> <img src="img/1.jpg" width="200"/> <p id="demo">点击按钮来改变img标签src属性的值。</p> <button onclick="myFunction()">试一下</button> <script> function myFunction() { document.getElementsByTagName("img")[0].src="img/3.jpg"; } </script> </body> </html>
效果图:
以上就是javascript怎么改变src属性值的详细内容,更多请关注自由互联其它相关文章!
本文共计414个文字,预计阅读时间需要2分钟。
JavaScript修改src属性值的方法:
1.使用setAttribute()方法,语法为:`元素对象.setAttribute('src', 属性值)`
2.利用HTML+DOM的src属性,语法为:`元素对象.src=属性值`
操作环境:Windows 7系统
javascript改变src属性值的方法:1、使用setAttribute()方法,语法“元素对象.setAttribute("src","属性值")”;2、利用HTML DOM的src属性,语法“元素对象.src="属性值"”。
本教程操作环境:windows7系统、javascript1.8.5版、Dell G3电脑。
javascript改变src属性值
方法1:使用setAttribute()方法
setAttribute() 方法添加指定的属性,并为其赋指定的值。
如果这个指定的属性已存在,则仅设置/更改值。
示例:
<!DOCTYPE html> <html> <body> <img src="img/1.jpg" width="200"/> <p id="demo">点击按钮来改变img标签src属性的值。</p> <button onclick="myFunction()">试一下</button> <script> function myFunction() { document.getElementsByTagName("img")[0].setAttribute("src","img/2.jpg"); } </script> </body> </html>
效果图:
2、使用HTML DOM对象的src属性
<!DOCTYPE html> <html> <body> <img src="img/1.jpg" width="200"/> <p id="demo">点击按钮来改变img标签src属性的值。</p> <button onclick="myFunction()">试一下</button> <script> function myFunction() { document.getElementsByTagName("img")[0].src="img/3.jpg"; } </script> </body> </html>
效果图:
以上就是javascript怎么改变src属性值的详细内容,更多请关注自由互联其它相关文章!

