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

How to change src in javascript

William Shakespeare
William Shakespeare Original
2021-04-13 15:40:55 7276browse

How to change src in javascript: First use the "document.getElementById("id value")" statement to obtain the element object according to the specified id value; then use the src attribute to change the src value of the element, the syntax is "element object. src="//m.sbmmt.com/m/faq/value";".

How to change src in javascript

The operating environment of this tutorial: Windows 7 system, JavaScript version 1.8.5, Dell G3 computer.

Create a new HTML file named test.html to explain how JavaScript can dynamically change the src attribute of the img tag.

How to change src in javascript

In the test.html file, use the img tag to create an image and set its id attribute to mypic, which is mainly used to obtain the image object using js below.

How to change src in javascript

In the test.html file, use the button tag to create a button with the button name "Change src attribute".

How to change src in javascript

In the test.html file, bind the onclick event to the button button. When the button is clicked, execute the cc() function.

How to change src in javascript

In the test.html file, in the js tag, create a cc() function. Within the function, use the getElementById() method to obtain the image object by id.

How to change src in javascript

In the cc() function, change the src attribute of the image by assigning a value to the src attribute of the img object. For example, set the src attribute of the image to 3.jpg .

How to change src in javascript

Open the test.html file in the browser and click the button to see the effect.

How to change src in javascript

How to change src in javascript

Recommended learning:javascript video tutorial

The above is the detailed content of How to change src 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
Previous article:How to set css style in jq Next article:How to set css style in jq