jquery can get the src attribute of the image. Obtaining method: 1. Use attr() to get the src attribute of the img picture element, the syntax is "$(img).attr("src")"; 2. Use prop() to get the first matching img picture element src attribute, the syntax is "$(img).prop("src")".
The operating environment of this tutorial: windows7 system, jquery3.6.1 version, Dell G3 computer.
In jquery, you can use two methods to get the attributes of an element:
attr()
prop()
Method 1. Use attr() to get the src attribute of the image
attr() method can return the Select the attribute value of the element
Syntax:
元素对象.attr("属性名")
You only need to get the specified img element object and use the
attr("src")
method. You can get the src attribute of the image
Example: Get the img src attribute value, which is the image address
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <script src="js/jquery-3.6.1.min.js"></script> <script type="text/javascript"> $(document).ready(function() { $("button").click(function() { var img=$("img").attr("src"); console.log("src属性值为: "+img) }); }); </script> </head> <body> <img src="/static/imghwm/default1.png" data-src="img/2.jpg" class="lazy" id="img" style="max-width:90%"/ alt="Can jquery get the src attribute of an image?" ><br><br> <button>获取img src属性值(图片地址)</button> </body> </html>
2. Use prop() to get the src attribute of the image
The prop() method can return the attribute value of the selected element, and will return the attribute value of the first matching element.
Syntax:
元素对象.prop("属性名")
You only need to get the specified img element object and use the
prop("src")
method to obtain it The src attribute of the image
Example: Get the img src attribute value, which is the image address
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <script src="js/jquery-3.6.1.min.js"></script> <script type="text/javascript"> $(document).ready(function() { $("button").click(function() { var img=$("img").prop("src");; console.log("src属性值为: "+img) }); }); </script> </head> <body> <img src="/static/imghwm/default1.png" data-src="img/2.jpg" class="lazy" id="img" style="max-width:90%"/ alt="Can jquery get the src attribute of an image?" ><br><br> <button>获取img src属性值(图片地址)</button> </body> </html>
[Recommended learning :jQuery video tutorial、web front-end video】
The above is the detailed content of Can jquery get the src attribute of an image?. For more information, please follow other related articles on the PHP Chinese website!

KeysinReactarecrucialforoptimizingperformancebyaidinginefficientlistupdates.1)Usekeystoidentifyandtracklistelements.2)Avoidusingarrayindicesaskeystopreventperformanceissues.3)Choosestableidentifierslikeitem.idtomaintaincomponentstateandimproveperform

Reactkeysareuniqueidentifiersusedwhenrenderingliststoimprovereconciliationefficiency.1)TheyhelpReacttrackchangesinlistitems,2)usingstableanduniqueidentifierslikeitemIDsisrecommended,3)avoidusingarrayindicesaskeystopreventissueswithreordering,and4)ens

UniquekeysarecrucialinReactforoptimizingrenderingandmaintainingcomponentstateintegrity.1)Useanaturaluniqueidentifierfromyourdataifavailable.2)Ifnonaturalidentifierexists,generateauniquekeyusingalibrarylikeuuid.3)Avoidusingarrayindicesaskeys,especiall

Using indexes as keys is acceptable in React, but only if the order of list items is unchanged and not dynamically added or deleted; otherwise, a stable and unique identifier should be used as the keys. 1) It is OK to use index as key in a static list (download menu option). 2) If list items can be reordered, added or deleted, using indexes will lead to state loss and unexpected behavior. 3) Always use the unique ID of the data or the generated identifier (such as UUID) as the key to ensure that React correctly updates the DOM and maintains component status.

JSXisspecialbecauseitblendsHTMLwithJavaScript,enablingcomponent-basedUIdesign.1)ItallowsembeddingJavaScriptinHTML-likesyntax,enhancingUIdesignandlogicintegration.2)JSXpromotesamodularapproachwithreusablecomponents,improvingcodemaintainabilityandflexi

The article discusses HTML5 audio formats and cross-browser compatibility. It covers MP3, WAV, OGG, AAC, and WebM, and suggests using multiple sources and fallbacks for broader accessibility.

SVG and Canvas are HTML5 elements for web graphics. SVG, being vector-based, excels in scalability and interactivity, while Canvas, pixel-based, is better for performance-intensive applications like games.

HTML5 enables drag and drop with specific events and attributes, allowing customization but facing browser compatibility issues on older versions and mobile devices.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 English version
Recommended: Win version, supports code prompts!

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.
