How Can I Change an Image Source Using jQuery?
Dec 23, 2024 am 02:38 AMModifying Image Source with jQuery
In a webpage, suppose you have an HTML structure with two images, each having "imgx_on.gif" as their source, where "x" represents an image number (1 or 2). Upon clicking an image, you wish to modify its source to "imgx_off.gif".
Solution with jQuery
Utilizing jQuery's attr() function, you can alter the source attribute of an image. To achieve this, assign an id attribute to your image (for instance, "my-image") and execute the following code:
$('#my-image').attr('src', 'imgx_off.gif');
Example for Image Rotation
To rotate images, you can employ the following jQuery code:
$('img').on({ 'click': function() { var src = ($(this).attr('src') === 'img1_on.gif') ? 'img2_on.gif' : 'img1_on.gif'; $(this).attr('src', src); } });
This code alternates the source of the clicked image between "img1_on.gif" and "img2_on.gif".
The above is the detailed content of How Can I Change an Image Source Using jQuery?. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Replace String Characters in JavaScript

Custom Google Search API Setup Tutorial

8 Stunning jQuery Page Layout Plugins
