How to set src attribute in javascript

青灯夜游
Release: 2023-01-05 16:12:00
Original
8602 people have browsed it

How to set the src attribute in javascript: first use the "document.getElementById("id value")" statement to get the element object; then use the "element object.setAttribute("src","URL address")" statement to add or modify element properties.

How to set src attribute in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

javascript sets src attribute

In javascript, you can use the setAttribute() method to set the src attribute of an element. The code example is as follows:

How to set src attribute in javascript 

点击按钮来设置img图片的 src属性。

Copy after login

Effect Figure:

How to set src attribute in javascript

Description: setAttribute() method

setAttribute() method adds the specified attribute and assigns it Assign the specified value. If this specified property already exists, the value is only set/changed.

Syntax

element.setAttribute(attributename,attributevalue)
Copy after login
Parameters Type Description
attributename String Required. The name of the attribute you wish to add.
attributevalue String Required. The attribute value you wish to add.

[Recommended learning:javascript advanced tutorial]

The above is the detailed content of How to set src attribute in javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!