Home  >  Article  >  Web Front-end  >  How to set src attribute in javascript

How to set src attribute in javascript

青灯夜游
青灯夜游Original
2021-04-27 16:04:578555browse

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属性。

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)
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!

Statement:
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