用jQuery获取相对图像src [英] Getting relative image src with jQuery
问题描述
我的图像标签看起来像这样:
I've got image tags that look like this:
<img src="/path/to/my/image.jpg" />
但是当我使用jQuery访问src位时,jQuery给了我回复:
But when I access the src bit using jQuery, jQuery gives me back:
http://example.com/path/to/my/image.jpg
这在我正在做的一些比较中引起了问题。我不想更改我的图像路径以使用绝对URL。
This is causing problems in some comparisons I'm doing. I don't want to change my image paths to use absolute URLs.
关于如何从图像路径获取绝对URL的任何想法(这可能不是就像连接域一样简单 - 因为URL有时可能是绝对的),或者获取我在HTML中提供的路径?无论哪种方式,我都需要他们匹配。
Any ideas as to how I can either get the absolute URL from the image path (this might not be as simple as concatenating the domain - since the URLs may occasionally be absolute), or get the path I provided in the HTML? Either way I need them to match up.
实际上并不是很多要发布的jQuery代码,我正在使用循环插件,并在中onbefore
函数,我只是调用 next.src
。我的jQuery和JavaScript foo不足以真正理解循环插件正在做什么来生成 next
- 我认为这是下一个图像循环的DOM元素,如果你熟悉什么周期。
There's not really a lot of jQuery code to post, I'm using the cycle plugin, and in the onbefore
function, I'm just calling next.src
. My jQuery and JavaScript foo isn't sufficient to really understand what the cycle plugin is doing to generate next
- I think it's the DOM element for the next image being cycled in, if you're familiar with what cycle does.
我的图像标签实际上就是这样:
My image tag is actually this:
<img src="/site_media/photologue/photos/cache/6927d406810ee9750a754606dcb61d28.jpg" alt="" class="landscape slideshow-image-1" />
以及我的 onbefore
函数此代码:
and in my onbefore
function this code:
alert(next.src);
会产生一个警告:
http://127.0.0.1:8000/site_media/photologue/photos/cache/6927d406810ee9750a754606dcb61d28.jpg
推荐答案
$(img)。attr(src)
返回实际值src属性(在FF3和IE7中尝试过)
$("img").attr("src")
gives back the actual value of the src attribute (tried in FF3 and IE7)
所以当你有
So when you have
<img src="http://example.com/path/to/my/image.jpg" />
它将返回
http://example.com/path/to/my/image.jpg
当你有
<img src="/path/to/my/image.jpg" />
它将返回
/path/to/my/image.jpg
编辑后编辑问题
听起来您可能想尝试
$(next).attr("src")
这篇关于用jQuery获取相对图像src的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!