Home > Article > Web Front-end > How to get the path of the image in javascript
Javascript method to obtain the image path: 1. Use the getAttribute() function, the syntax is "image object.getAttribute('src')"; 2. Use the src attribute of the Image object
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
In HTML, images are inserted through the src attribute of the img tag. The src attribute specifies the URL path to display the image.
Using javascript to obtain the image path is to use javascript to obtain the value of the src attribute of the img tag. Two methods are introduced below:
Method 1: Use the getAttribute() function
The getAttribute() method gets the value of the attribute by name.
Syntax:Picture object.getAttribute('src')
Example:
<img id="img" src="https://img.php.cn/upload/article/000/000/024/6167dde0cdd4c763.jpg" / alt="How to get the path of the image in javascript" > <script type="text/javascript"> var img=document.getElementById("img"); console.log(img.getAttribute("src")); </script>
Output result:
Method 2: Use the src attribute of the Image object
Image object.src
<img id="img" src="https://img.php.cn/upload/article/000/000/024/6167dde0cdd4c763.jpg" / alt="How to get the path of the image in javascript" > <script type="text/javascript"> var img=document.getElementById("img"); console.log(img.src); </script>Output result: [Recommended learning:
javascript advanced tutorial]
The above is the detailed content of How to get the path of the image in javascript. For more information, please follow other related articles on the PHP Chinese website!