Home > Web Front-end > JS Tutorial > How to change the src attribute of img element in jquery

How to change the src attribute of img element in jquery

藏色散人
Release: 2020-12-15 14:12:54
Original
6650 people have browsed it

Jquery method to change the src attribute of the img element: First, bind the onclick click event to the button button in the html file; then create the editsrc function in the js tag; then obtain the img through "id (myimg)" Object; finally use the attr method to set the src attribute.

How to change the src attribute of img element in jquery

The operating environment of this tutorial: windows7 system, jquery1.10.0 version. This method is suitable for all brands of computers.

Recommended: "jquery video tutorial"

In jquery, the src attribute of the img tag is mainly set through the attr() method, thereby changing the src of the img tag. Attributes.

Method: First you need to get the img element, and then use .attr("src","value") to change the src attribute of the img element.

Detailed introduction:

In the test.html file, bind the onclick click event to the button button. When the button is clicked, the editsrc() function is executed.

How to change the src attribute of img element in jquery

In the js tag, create the editsrc() function. Within the function, use the $ symbol to obtain the img object through id(myimg), and use the attr() method to set the src attribute. , for example, set the src image path to "images/3.jpg".

How to change the src attribute of img element in jquery

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

How to change the src attribute of img element in jquery

The above is the detailed content of How to change the src attribute of img element in jquery. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template