Home > Web Front-end > Front-end Q&A > jquery sets the src attribute value for img

jquery sets the src attribute value for img

王林
Release: 2023-05-23 10:49:06
Original
6697 people have browsed it

In web pages, it is often necessary to display images on the page. In this case, you need to use the tag. Among them, the src attribute of the tag is used to specify the resource path of the image. When developing with jQuery, we can dynamically set the src attribute value of the tag in the following ways.

1. Use the attr method

Using jQuery's attr method, you can easily get and set the attribute values ​​​​of HTML elements. When setting the src attribute of the tag, we can use the attr method in the following way:

$("img").attr("src", "image.jpg")
Copy after login

In the above code, we obtain all tags in the current page through the jQuery selector , and then set their src attribute to "image.jpg" through the attr method.

Of course, you can also select specific tags through other attributes, for example:

$("img.thumbnail").attr("src", "image.jpg")
Copy after login

In the above code, we select all< whose class name is "thumbnail" img> tag and set its src attribute to "image.jpg".

2. Use the prop method

The prop method is mainly used to get and set the attribute values ​​​​of HTML elements, but the difference is that it is used to set Boolean attributes. For the src attribute of the tag, the prop method can also be used. For example:

$("img").prop("src", "image.jpg")
Copy after login

Similar to the attr method, we can also select specific tags through other attributes, for example:

$("img.thumbnail").prop("src", "image.jpg")
Copy after login

3. Use direct assignment

In addition to using the attr method and prop method, we can also set the src attribute of the tag directly through JavaScript. For example:

$("img")[0].src = "image.jpg"
Copy after login

In the above code, we obtain the first tag through the jQuery selector, and set its src attribute to "image.jpg" directly through JavaScript.

Summary

Through the above methods, we can easily set the src attribute of the tag dynamically. Among them, the attr method and the prop method are simpler and easier to understand, and are more in line with jQuery's programming ideas. Although the direct assignment method can achieve the same effect, it is slightly inconsistent with jQuery's programming ideas. Therefore, when developing with jQuery, it is recommended to use the attr method or prop method to set the attribute value of HTML elements.

The above is the detailed content of jquery sets the src attribute value for img. For more information, please follow other related articles on the PHP Chinese website!

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