通过Javascript查找图像的src(Finding the src of an image via Javascript)

编程入门 行业动态 更新时间:2024-10-28 20:25:26
通过Javascript查找图像的src(Finding the src of an image via Javascript)

我通过一个简单的谷歌搜索找到了我的问题的答案,但是,我遇到了一个小问题。

到目前为止,我的HTML页面上有一个图像,其ID为newAvatar。 我试图通过它的ID找到这个图像,并将它的源保存到一个变量中,然后最终变成一个XML文档,以便以后使用。

当我将此源保存到变量中,或者将其警告以进行检查时,它会显示完整的源,这是不寻常的。 例如,我在WAMP服务器上运行我的站点,因此变量保存为:

http://localhost/uploads/newUploadedImage.jpg

实际上,源代码应该简单明了,uploads / newUploadedImage.jpg

图片标签是

<img src="uploads/newUploadedImage.jpg" width="60px" height="60px;" id="newAvatar"/>

我检查来源的方式是

alert(document.getElementById('newAvatar').src);

任何想法如何摆脱第一批垃圾?

非常感谢所有帮助!

I found the answer to my question via a simple Google search, although, there is a slight problem that I ran into.

So far, I have an image on my HTML page with the ID of newAvatar. I am attempting to find this image via its ID, and save it's source into a variable, then eventually into an XML document to be used later on.

When I save this source into a variable, or alert it for checking purposes, it is displaying the full source, which is unusual. For example, I am running my site on a WAMP server, so the variable is saving as:

http://localhost/uploads/newUploadedImage.jpg

When in reality, the source should be plain and simple, uploads/newUploadedImage.jpg

The image tag is

<img src="uploads/newUploadedImage.jpg" width="60px" height="60px;" id="newAvatar"/>

The way i am checking the source is

alert(document.getElementById('newAvatar').src);

Any idea how to get rid of the first lot of junk?

All help is greatly appreciated!

最满意答案

通过之前使用@DavidThomas评论,我设法让它工作。 这就是我将警报更改为:

alert(document.getElementById('newAvatar').getAttribute('src'));

By using @DavidThomas comment before, I managed to get it working. This is what i changed my alert to:

alert(document.getElementById('newAvatar').getAttribute('src'));

更多推荐

source,uploads,newUploadedImage,电脑培训,计算机培训,IT培训"/> <meta name=&qu

本文发布于:2023-07-22 02:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1216287.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   Javascript   src   image   Finding

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!